C++14:先前使用固定基础类型声明的枚举:编译器之间的不同行为
C++14: enumeration previously declared with fixed underlying type: different behavior between compilers
示例代码:
enum E : char;
enum E;
调用:
$ g++ -std=c++14 -pedantic -Wall -Wextra -c
<nothing>
$ clang++ -std=c++14 -pedantic -Wall -Wextra -c
<source>:2:6: error: enumeration previously declared with fixed underlying type
$ icc -std=c++14 -pedantic -Wall -Wextra -c
<nothing>
$ cl /std:c++14 /Za
<source>(2): error C3432: 'E': a forward declaration of an unscoped enumeration must have an underlying type
<source>(2): error C3433: 'E': all declarations of an enumeration must have the same underlying type, was 'char' now 'int'
这段代码格式正确吗?
标准是怎么说的?
来自[dcl.enum]/3:
An unscoped enumeration shall not be later redeclared as scoped and each redeclaration shall include an enum-base specifying the same underlying type as in the original declaration.
添加了重点。 Clang 是正确的。
示例代码:
enum E : char;
enum E;
调用:
$ g++ -std=c++14 -pedantic -Wall -Wextra -c
<nothing>
$ clang++ -std=c++14 -pedantic -Wall -Wextra -c
<source>:2:6: error: enumeration previously declared with fixed underlying type
$ icc -std=c++14 -pedantic -Wall -Wextra -c
<nothing>
$ cl /std:c++14 /Za
<source>(2): error C3432: 'E': a forward declaration of an unscoped enumeration must have an underlying type
<source>(2): error C3433: 'E': all declarations of an enumeration must have the same underlying type, was 'char' now 'int'
这段代码格式正确吗?
标准是怎么说的?
来自[dcl.enum]/3:
An unscoped enumeration shall not be later redeclared as scoped and each redeclaration shall include an enum-base specifying the same underlying type as in the original declaration.
添加了重点。 Clang 是正确的。