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

      <tfoot id='EYNyn'></tfoot>

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

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

        “枚举类"MSVC 10.0 的仿真或可靠替代方案

        时间:2023-07-02
          <tbody id='HbI7A'></tbody>

            <bdo id='HbI7A'></bdo><ul id='HbI7A'></ul>

              <tfoot id='HbI7A'></tfoot>

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

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

                  本文介绍了“枚举类"MSVC 10.0 的仿真或可靠替代方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在寻找一种解决以下问题的方法:GCC 4.4+ 接受以下 c++0x 代码:

                  枚举类 my_enum{值1,值2};

                  允许这样使用:

                  my_enum e = my_enum::value1;

                  这带来了所有的花里胡哨.我想让此代码与 MSVC 2010 兼容,从而使使用语法不会改变.我之前已经考虑过这个问题 在这里,并且接受的答案有效,但是需要枚举和枚举值的两个不同名称正在扼杀这两种方法的兼容性.这当然无法按原样替换 C++0x 代码.我想知道是否一些 #undef#define 技巧可以解决这个问题,允许我use enum class-像语法(也许没有严格的类型安全等),但至少是相同的语法.谢谢!

                  解决方案

                  我刚刚发现 James 的 good hack(我之前一直在使用)的一个问题,并修复了该问题.我在尝试为 my_enum 定义流运算符时发现了这个问题.

                  #include 结构 my_enum {枚举类型{值1,值2};my_enum(type v) : value_(v) { }运算符 type() const { return value_;}私人的:类型值_;};std::ostream&运算符<<(std::ostream& os, my_enum v){返回操作系统<<流式传输 my_enum";}int main(){std::cout <<my_enum::value1 <<'
                  ';}

                  输出为:

                  <预><代码>0

                  问题是 my_enum::value1my_enum 的类型不同.这是我想出的对 James hack 的一个hack.

                  struct my_enum{static const my_enum value1;static const my_enum value2;显式 my_enum(int v) : value_(v) { }//明确的//如果你有的话!运算符 int() const { 返回值_;}私人的:整数值_;};my_enum const my_enum::value1(0);my_enum const my_enum::value2(1);

                  注意事项:

                  1. 除非 enum-base 另有规定,作用域枚举的基础类型是 int.
                  2. 允许与基础整数类型之间的显式转换.但隐式转换不是.尽力而为.
                  3. 这个 hack 比 James 的更像是一个皮塔饼,因为需要枚举值两次.我希望没有作用域枚举支持的编译器迅速消失!

                  I'm looking for a hacky kind of solution to the following problem: GCC 4.4+ accepts the following c++0x code:

                  enum class my_enum
                  {
                      value1,
                      value2
                  };
                  

                  Which allows the use like this:

                  my_enum e = my_enum::value1;
                  

                  with all the bells and whistles this brings. I would like to make this code compatible with MSVC 2010, to the effect that the usage syntax does not change. I already pondered on this before here, and the accepted answer works, but the need for the two different names fo the enum and the enum values is killing the compatibility of the two approaches. This makes it of course unusable to replace the C++0x code as is. I wondered if some #undef and #define trickery could work around this, allowing me to use enum class-like syntax (perhaps without the strict type safety etc.), but at least the same syntax. Thanks!

                  解决方案

                  I just discovered a problem with James' good hack (which I have heretofore been using), and a fix to the problem. I discovered the problem when I tried to define a stream operator for my_enum.

                  #include <iostream>
                  
                  struct my_enum {
                      enum type { 
                          value1, 
                          value2 
                      };
                  
                      my_enum(type v) : value_(v) { }
                  
                      operator type() const { return value_; }
                  
                  private:
                  
                      type value_;
                  };
                  
                  std::ostream&
                  operator<<(std::ostream& os, my_enum v)
                  {
                      return os << "streaming my_enum";
                  }
                  
                  int main()
                  {
                      std::cout << my_enum::value1 << '
                  ';
                  }
                  

                  The output is:

                  0
                  

                  The problem is my_enum::value1 has different type than my_enum. Here's a hack to James' hack that I came up with.

                  struct my_enum
                  {
                      static const my_enum value1;
                      static const my_enum value2;
                  
                      explicit my_enum(int v) : value_(v) { }
                  
                      // explicit // if you have it!
                         operator int() const { return value_; }
                  
                  private:
                  
                      int value_;
                  };
                  
                  my_enum const my_enum::value1(0);
                  my_enum const my_enum::value2(1);
                  

                  Notes:

                  1. Unless otherwise specified by an enum-base, the underlying type of a scoped enumeration is int.
                  2. Explicit conversions to and from the underlying integral type are allowed. But implicit conversions are not. Do your best.
                  3. This hack is more of a pita than James' because of the need to enumerate the values twice. I'm hoping compilers without scoped enum support rapidly become extinct!

                  这篇关于“枚举类"MSVC 10.0 的仿真或可靠替代方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:我可以禁用 STL 中的异常吗? 下一篇:错误请#define _AFXDLL 或不使用/MD[d] 即使在项目属性中进行更改后也会发生

                  相关文章

                  1. <small id='TBSZL'></small><noframes id='TBSZL'>

                  2. <legend id='TBSZL'><style id='TBSZL'><dir id='TBSZL'><q id='TBSZL'></q></dir></style></legend>

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