C++11 访问具有限定名称的无作用域枚举数

标签 c++ c++11 enums language-lawyer

我对 C++11 标准的措辞有疑问,因为我过去不必经常深入研究它,最近发现自己对无范围枚举的(公认不重要的)主题感到困惑。

我最近在代码审查中遇到了一些代码,这些代码使用无范围枚举但使用完全限定名称访问枚举数,如下所示:

enum SomeEnum
{
  EnumA,
  ...
};

void foo()
{
  SomeEnum x = SomeEnum::EnumA;
}

我确信这行不通,并且 SomeEnum 必须是这种行为的枚举类,但是,果然,它编译得很干净。

窥视 C++11 标准,起初我认为该标准与我一致:

§ 7.2 Enumeration Declarations: 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.

在我看来,这表明无范围枚举器仅在枚举本身的直接包含范围内声明。它没有提到它们也在枚举范围内声明。

但是,再往下看,该标准确实包含一个示例,该示例显示使用完全限定名称访问无作用域的枚举器。

在 SO 上快速搜索和搜索给了我一小部分断言标准现在允许完全限定名称的地方,但没有太多讨论。这只是示例中阐明的规范中的薄弱措辞,还是我遗漏了其他内容?

再说一次,这不是惊天动地的,但我希望有人可以让我直接了解标准,我可以学到一些可能在未来情况下有用的东西。

最佳答案

您正在寻找的报价来自draft C++11 standard来自 5.1 Primary expressions 部分,在 10 段中说:

A nested-name-specifier that denotes an enumeration (7.2), followed by the name of an enumerator of that enumeration, is a qualified-id that refers to the enumerator. The result is the enumerator. The type of the result is the type of the enumeration. The result is a prvalue.

它不限制对作用域枚举的使用,因此 7.2 部分中的示例 枚举声明:

enum direction { left='l', right='r' };

void g() {
   direction d; // OK
   d = left; // OK
   d = direction::right; // OK
}

是完全一致的。这也与 3.4.3 Qualified name lookup 部分一致,其中说:

The name of a class or namespace member or enumerator can be referred to after the :: scope resolution operator (5.1) applied to a nested-name-specifier that denotes its class, namespace, or enumeration. [...]

和:

A name prefixed by a nested-name-specifier that nominates an enumeration type shall represent an enumerator of that enumeration.

同样,没有任何东西将这种行为限制在作用域枚举中。

Examples are non normative但只要它们不与规范性文本冲突,它们就应该被视为一个很好的指南。

关于C++11 访问具有限定名称的无作用域枚举数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26744528/

相关文章:

java - 为什么类中的公共(public)枚举类型不能在单独的主类中初始化?

c - 了解标识符的链接

c++ - 我可以从基于范围的 for 中 move 元素吗?

c++ - 了解格子

c++ - std::stack 元素销毁顺序

c++ - 将字符与 Qt 中的 unicode 进行比较

c# - 在 C# 中使用反射自注册工厂

c++ - C++ 对象是否应该始终处于有效状态?

c++ - 将 AVX 与 GCC : __builtin_ia32_addpd256 not declared 一起使用

c++ - 如何在 C++ 正则表达式中捕获 0-2 组并打印它们?