“枚举类” MSVC 10.0的仿真或固体替代品 [英] "enum class" emulation or solid alternative for MSVC 10.0

查看:146
本文介绍了“枚举类” MSVC 10.0的仿真或固体替代品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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
};

其中允许使用如下:

my_enum e = my_enum::value1;

带来所有的铃声和​​口哨。我想使这个代码与MSVC 2010兼容,使得语法不会改变。在之前,我已经考虑过了这里,并且接受的答案有效,但对枚举和枚举值的两个不同名称的需求正在杀死这两种方法的兼容性。这使得它当然不能用于替换C ++ 0x代码。我想知道一些 #undef #define 诡计是否可以解决这个问题,允许我使用 枚举类类语法(可能没有严格的类型安全等),但至少是相同的语法。谢谢!

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!

推荐答案

我刚刚发现了James的好黑客(我以前一直在使用的)的一个问题,问题。当我尝试为my_enum定义流运算符时,我发现了这个问题。

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 << '\n';
}

输出是:

0

问题是 my_enum :: value1 的类型与 my_enum 不同。这是我想到的James的黑客攻击。

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);

注意:


  1. 除非由枚举基础另有指定,否则作用域枚举的基础类型为 int

  2. 允许从底层积分类型进行显式转换。但隐式转换不是。最好的。

  3. 由于需要两次枚举这个值,这个黑客比皮亚饼更像是詹姆斯。我希望编译器没有范围的枚举支持迅速灭绝!

  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的仿真或固体替代品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆