具有作用域枚举的位集无法编译

bitset with scoped enums doesn't compile

我正在尝试将 std::bitsetenum 一起使用,但出现编译错误

模板参数 1 无效

有趣的是,当我在没有枚举范围的情况下使用任何枚举值时,它工作正常。

你知道为什么吗?

代码下方

   enum MyTypes {
     Alpha  = 1,
     Beta  = 2,
     Gamma = 3
   };


   std::bitset<MyTypes::Alpha> bitset_wrong; // It doesn't compile.
   std::bitset<Alpha >         bitset_good;  // It works.

您的旧编译器似乎不支持使用无作用域枚举数指定限定名称。

更新你的编译器。:)

根据 C++ 2011 标准,您显示的代码是有效代码。

这里引用了 C++ 标准中的一个例子(7.2 枚举声明)

11 Each enum-name and each unscoped enumerator is declared in the scope that immediately contains the enum-specifier. Each scoped enumerator is declared in the scope of the enumeration. These names obey the scope rules defined for all names in (3.3) and (3.4).

[示例:

enum direction { left=’l’, right=’r’ };
void g() {
direction d; // OK
d = left; // OK
d = direction::right; // OK
}