gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 02:21:21 24 4
gpt4 key购买 nike

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com