枚举类型声明的意义何在?它是否紧跟在枚举名称之后?我看到了标准 C++ 14 (n4296) §3.3.2/3:
The point of declaration for an enumeration is immediately after the identifier (if any) in either its enum-specifier (7.2) or its first opaque-enum-declaration (7.2), whichever comes first
但是当我尝试复制它时;
template <class T>
struct CL
{
using UndType = int;
};
enum class E: CL<E>::UndType; //error: E is undefined
我在所有编译器上都遇到了错误,尽管枚举 E
的 enum-base 放在标识符之后并且必须可见。
最佳答案
以下;
enum class E : CL<E>::UndType;
在某些当前实现(测试过的 clang++、g++ 和 MSVC)中不被接受为有效声明。他们不接受尚不完整的类型 E
, 在 枚举基础 CL<E>::UndType
.测试实现中给出的错误是 E
那时是未声明。他们似乎将声明点放在 enum-base 的末尾,他们认为它一旦完成就声明了。
阅读规范时;
§14.3.1/2 模板类型参数
[ Note: A template type argument may be an incomplete type (3.9). — end note ]
还有
§7.2/6 枚举声明
An enumeration whose underlying type is fixed is an incomplete type from its point of declaration (3.3.2) to immediately after its enum-base (if any), at which point it becomes a complete type.
是否暗示它是可编译的;与 CRTP 实现一样。
我注意到这(即无法编译 enum class E : CL<E>::UndType;
)是否是意图,或者它是否被视为用例。从规范中,不透明的枚举声明被给予了一些“特殊”的处理w.r.t。它的基类型以及它必须是整数类型的要求。
假设,代码应该是可编译的,如果分辨率为CWG#1482 .
至于目前的解决方法...
这个;
enum class E; // default underlying type is int
是最小的声明。
不透明的声明可以是;
enum class E : int; // int base
以下是完整的定义(包括枚举数);
enum class E : int {/*...*/};
或者要使用类模板,可以使用其他类型(可能是 void
)。
enum class E : CL<void>::UndType;
关于c++ - 枚举声明点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35695071/