C++14(准确地说,N4296)在 7.2:11 中提到了枚举:
Each enum-name and each unscoped enumerator is declared in the scope that immediately contains the enum-specifier.
现在,如果命名空间 N 包含枚举 E 的不透明枚举声明,然后该枚举完全从全局命名空间声明,会发生什么?我们应该在全局命名空间中,还是在命名空间 N 中找到它的枚举数?
当然,为了不透明地声明一个无范围的枚举,它应该有一个固定的底层类型。考虑以下代码。
namespace N { enum E : int; }
enum N::E : int {A,B};
namespace N {
int foo() {
return int(::N::B);
}
}
int bar() {
//return int(::A);
return int(A);
}
bar
中的第一行被注释掉了,因为clang++ -std=c++14
说:
no member named 'A' in the global namespace; did you mean simply 'A'?
Gcc 无法编译 bar() 中的两行。所以 gcc 和 clang 都在命名空间 N
.
所以我的问题是:
- 立即包含枚举说明符的范围是什么? (我认为这是全局命名空间的范围)。
- 枚举器
A
、B
是否应该在全局命名空间中定义? - 在
bar
函数中,为什么::A
没有引用枚举数,而简单的A
有? - 为什么函数
N::foo
中的表达式::N::B
表示枚举数?
编辑 1:原始声明是 enum::N::E : int {A,B};
,但 gcc 无法解析它(bug report),所以我删除了要使用的前导冒号 enum N::E : int {A,B};
编辑 2:clang 的行为是 bug
最佳答案
枚举 E 在命名空间 N 中声明,即使它的定义是在全局命名空间中设置的。所以只能在N范围内访问。
然后应该将 bar 函数定义为:
int bar() {
return int(N::A);
//SAME AS --> return int(::N::A);
}
关于c++ - 在 C++14 中,在哪个范围内声明了重新声明的枚举的无范围枚举数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47383199/