我用 GCC 7.2.0 编译它:
typedef float Signal_t;
enum class Signal_level : Signal_t {
low = -1.0, neutral = 0.0, high = 1.0
};
编译器响应是:
error: underlying type ‘Signal_t {aka float}’ of ‘Signal_level’ must be an integral type
此行为是由标准 (-std=c++17
) 规定的还是特定于 GCC?
我希望 GCC 将 Signal_t
识别为整数类型。
最佳答案
根据 [dcl.enum]p2 :
The type-specifier-seq of an enum-base shall name an integral type; any cv-qualification is ignored.
这里的type-specifier-seq指的是:
之后的部分。
什么是整数类型([basic.types]p7):
Types bool, char, char16_t, char32_t, wchar_t, and the signed and unsigned integer types are collectively called integral types.47 A synonym for integral type is integer type.
所以 float 不是整数类型的一部分,因此在枚举中用作基数是无效的。
关于c++ - 枚举类的基础类型别名为整数类型(编译错误),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48447703/