<i id='jE7Jj'><tr id='jE7Jj'><dt id='jE7Jj'><q id='jE7Jj'><span id='jE7Jj'><b id='jE7Jj'><form id='jE7Jj'><ins id='jE7Jj'></ins><ul id='jE7Jj'></ul><sub id='jE7Jj'></sub></form><legend id='jE7Jj'></legend><bdo id='jE7Jj'><pre id='jE7Jj'><center id='jE7Jj'></center></pre></bdo></b><th id='jE7Jj'></th></span></q></dt></tr></i><div id='jE7Jj'><tfoot id='jE7Jj'></tfoot><dl id='jE7Jj'><fieldset id='jE7Jj'></fieldset></dl></div>

      <small id='jE7Jj'></small><noframes id='jE7Jj'>

      • <bdo id='jE7Jj'></bdo><ul id='jE7Jj'></ul>
        <tfoot id='jE7Jj'></tfoot>
        <legend id='jE7Jj'><style id='jE7Jj'><dir id='jE7Jj'><q id='jE7Jj'></q></dir></style></legend>

        C++,我可以在编译时静态初始化 std::map 吗?

        时间:2024-05-11
          <tbody id='7GBsz'></tbody>

              <tfoot id='7GBsz'></tfoot>
            1. <i id='7GBsz'><tr id='7GBsz'><dt id='7GBsz'><q id='7GBsz'><span id='7GBsz'><b id='7GBsz'><form id='7GBsz'><ins id='7GBsz'></ins><ul id='7GBsz'></ul><sub id='7GBsz'></sub></form><legend id='7GBsz'></legend><bdo id='7GBsz'><pre id='7GBsz'><center id='7GBsz'></center></pre></bdo></b><th id='7GBsz'></th></span></q></dt></tr></i><div id='7GBsz'><tfoot id='7GBsz'></tfoot><dl id='7GBsz'><fieldset id='7GBsz'></fieldset></dl></div>
            2. <small id='7GBsz'></small><noframes id='7GBsz'>

                  <bdo id='7GBsz'></bdo><ul id='7GBsz'></ul>
                  <legend id='7GBsz'><style id='7GBsz'><dir id='7GBsz'><q id='7GBsz'></q></dir></style></legend>
                • 本文介绍了C++,我可以在编译时静态初始化 std::map 吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  如果我编码这个

                  std::map<int, char> example = {
                                                  (1, 'a'),
                                                  (2, 'b'),
                                                  (3, 'c') 
                                                };
                  

                  然后g++对我说

                  deducing from brace-enclosed initializer list requires #include <initializer_list>
                  in C++98 ‘example’ must be initialized by constructor, not by ‘{...}’   
                  

                  这让我有点恼火,因为构造函数是运行时的,理论上可能会失败.

                  and that annoys me slightly because the constructor is run-time and can, theoretically fail.

                  当然,如果确实如此,它会很快失败并且应该始终如一地这样做,因此我应该快速定位 &纠正问题.

                  Sure, if it does, it will fail quickly and ought to do so consistently, so that I ought to quickly locate & correct the problem.

                  但是,我仍然很好奇 - 有没有在编译时初始化地图、向量等?

                  But, still, I am curious - is there anyway to initialize map, vector, etc, at compile time?

                  我应该说我正在为嵌入式系统开发.并非所有处理器都有 C++0x 编译器.最流行的可能会,但我不想遇到陷阱&必须维护 2 个版本的代码.

                  I should have said that I am developing for embedded systems. Not all processors will have a C++0x compiler. The most popular probably will, but I don't want to encounter a gotcha & have to maintain 2 versions of the code.

                  至于 Boost,我还没有决定.他们对在嵌入式系统中使用他们的有限状态机类是一厢情愿的,所以这实际上是我在这里编码的,事件/状态/Fsm 类.

                  As to Boost, I am undecided. They are wishy-washy on the use of their Finite State Machine classes in embedded systems, so that is actually what I am coding here, Event/State/Fsm classes.

                  唉,我想我还是谨慎一点为好,但我希望这个讨论对其他人有所帮助.

                  Sigh, I guess I'd better just play it safe, but I hope that this discussion has been helpful for others.

                  推荐答案

                  不在 C++98 中.C++11 支持这一点,因此如果您启用 C++11 标志并包含 g++ 建议的内容,则可以.

                  Not in C++98. C++11 supports this, so if you enable C++11 flags and include what g++ suggests, you can.

                  从 gcc 5 C++11 默认开启

                  from gcc 5 C++11 is on by default

                  这篇关于C++,我可以在编译时静态初始化 std::map 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:关于使用用户定义方法扩展 C++ STL 容器的更好方法的建议 下一篇:递增迭代器:++it 比 it++ 更有效吗?

                  相关文章

                    <tfoot id='QXkk3'></tfoot>

                    1. <i id='QXkk3'><tr id='QXkk3'><dt id='QXkk3'><q id='QXkk3'><span id='QXkk3'><b id='QXkk3'><form id='QXkk3'><ins id='QXkk3'></ins><ul id='QXkk3'></ul><sub id='QXkk3'></sub></form><legend id='QXkk3'></legend><bdo id='QXkk3'><pre id='QXkk3'><center id='QXkk3'></center></pre></bdo></b><th id='QXkk3'></th></span></q></dt></tr></i><div id='QXkk3'><tfoot id='QXkk3'></tfoot><dl id='QXkk3'><fieldset id='QXkk3'></fieldset></dl></div>
                    2. <legend id='QXkk3'><style id='QXkk3'><dir id='QXkk3'><q id='QXkk3'></q></dir></style></legend>
                      • <bdo id='QXkk3'></bdo><ul id='QXkk3'></ul>

                      <small id='QXkk3'></small><noframes id='QXkk3'>