integer常量太大,以至于在为枚举分配最大类型值时它是无符号的



这是我的枚举声明:

enum connection_primary_identifier_e : uint64_t
{
    INVALID_IDENTIFIER = std::numeric_limits<std::underlying_type<connection_primary_identifier_e>::type>::max(),
}

(如果我直接使用uint64_t作为类型,如果我使用-1-1ULL,也会发生同样的情况)

当我试图编译文件时,我会收到以下错误/警告:

error: integer constant is so large that it is unsigned [-Werror]
error: narrowing conversion of ‘18446744073709551615I128’ from ‘__int128’ to ‘unsigned int’ inside { } [-Werror=narrowing]
error: large integer implicitly truncated to unsigned type [-Werror=overflow]
cc1plus: all warnings being treated as errors

真正奇怪的是,错误实际上是为另一个文件(使用枚举)上不存在的行(文件最后一行之后的行号是3)产生的,我确保它没有缺少括号或类似的东西。

更新:使用uint32_t不会产生错误。

使用g++(GCC)4.8.3

可能是因为std::underlying_type最初未指定,不需要完整的类型。这无意中正好允许了这个代码,它使用了connection_primary_identifier_e,而它仍然是不完整的。

从C++17开始,您的代码肯定是非法的。

相关内容

最新更新