c++ - 为什么 clang 不允许通过实例访问嵌套的枚举类?

标签 c++ clang language-lawyer

考虑以下代码:

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/

相关文章:

c++ - opencv 保存从网络摄像头捕获的图像

c++11 - 这个代码有效还是无效? GCC 和 Clang 不同意

常量 32768 和 0x8000 之间的类型差异会有所不同吗?

clang - 在 C 代码中使用 memcpy 并使用 wasm 目标进行编译时出现 llvm-link 错误

c++ - 常量表达式中引用类型的变量

c++ - 覆盖虚函数时的奇怪语法

c++ - 在 map C++ 中使用列表

c++ - 断言(0)是什么意思?

c++ - 定义静态常量结构

c++ - Xcode 4.5 和 OpenMP with Clang (Apple LLVM) 仅使用一个内核