考虑以下代码:
struct S {
enum class EnumClass {
one
} ;
enum Enum {
one
};
};
int main()
{
S s;
S::EnumClass e = s.EnumClass::one; // <- Doesn't compile with clang
S::Enum e11 = s.Enum::one;
S::Enum e12 = s.one;
}
一切都适用于 GCC,但 clang(既不是 3.8 也不是 3.9)不编译 s.EnumClass::one
,给出错误:'S::EnumClass::one' is不是“S”类的成员
。
鉴于无范围枚举工作正常的事实,这似乎是一个错误。
最佳答案
这是 gcc1 中的错误。相关措辞在[expr.ref]p2 :
In either case, the id-expression [here:
EnumClass::one
] shall name a member of the class or of one of its base classes.
EnumClass::one
不是指类的成员,它是枚举的成员。该枚举是否属于该类并不重要,只是成员 one
本身不是该类的一部分。
但是 Enum::one
是类的一部分吗?是的,根据 [class.member]p1 :
Members of a class are data members, member functions, nested types, enumerators, and member templates and specializations thereof.
并在 [class.member]p3 中更加明确:
The enumerators of an unscoped enumeration defined in the class are members of the class.
1:我什至会把它称为标准中的错误,因为似乎没有任何(技术)理由禁止这样做,我认为它允许它是个好主意。虽然有讨论允许它,所以让我们看看委员会的决定。
关于c++ - 为什么 clang 不允许通过实例访问嵌套的枚举类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50027942/