C++ 中的 64 位枚举? [英] 64 bit enum in C++?
问题描述
有没有办法在 C++ 中使用 64 位枚举?在重构一些代码时,我遇到了一堆 #defines,它们作为枚举会更好,但大于 32 位会导致编译器出错.
Is there a way to have a 64 bit enum in C++? Whilst refactoring some code I came across bunch of #defines which would be better as an enum, but being greater than 32 bit causes the compiler to error.
出于某种原因,我认为以下可能有效:
For some reason I thought the following might work:
enum MY_ENUM : unsigned __int64
{
LARGE_VALUE = 0x1000000000000000,
};
推荐答案
我认为 C++98 不可能做到这一点.枚举的底层表示取决于编译器.在这种情况下,您最好使用:
I don't think that's possible with C++98. The underlying representation of enums is up to the compiler. In that case, you are better off using:
const __int64 LARGE_VALUE = 0x1000000000000000L;
从 C++11 开始,可以使用枚举类来指定枚举的基类型:
As of C++11, it is possible to use enum classes to specify the base type of the enum:
enum class MY_ENUM : unsigned __int64 {
LARGE_VALUE = 0x1000000000000000ULL
};
此外,枚举类引入了一个新的名称范围.因此,不是引用 LARGE_VALUE
,而是引用 MY_ENUM::LARGE_VALUE
.
In addition enum classes introduce a new name scope. So instead of referring to LARGE_VALUE
, you would reference MY_ENUM::LARGE_VALUE
.
这篇关于C++ 中的 64 位枚举?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!