c++ - 枚举类的基础类型别名为整数类型(编译错误)

标签 c++ gcc types enums underlyingtype

我用 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/

相关文章:

c++ - 编写用于创建 vector 长度 vector 的算法

c++ - 了解 makefile

c++ - 带有模板的头文件中出现 "not declared in scope"错误

c - GCC 动态链接 libc static 和其他一些库,重新访问了吗?

带参数的 typescript 枚举

c++ - Linux BTF : bpftool: Failed to get EHDR from/sys/kernel/btf/vmlinux

linux - ld链接失败

c - 错误 : conflicting types for built-in function ‘tolower’ [-Werror]

r - 确定数据框列的数据类型

c# - 从通用类型问题推断