“枚举类"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::value1
与 my_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);
注意事项:
- 除非 enum-base 另有规定,作用域枚举的基础类型是
int
. - 允许与基础整数类型之间的显式转换.但隐式转换不是.尽力而为.
- 这个 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:
- Unless otherwise specified by an enum-base, the underlying type of a scoped enumeration is
int
. - Explicit conversions to and from the underlying integral type are allowed. But implicit conversions are not. Do your best.
- 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!
相关文章