C++ 中的 64 位枚举?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/76624/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 12:40:47  来源:igfitidea点击:

64 bit enum in C++?

c++enums64-bit

提问by Rob

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.

有没有办法在 C++ 中使用 64 位枚举?在重构一些代码时,我遇到了一堆 #defines,它们作为枚举会更好,但大于 32 位会导致编译器出错。

For some reason I thought the following might work:

出于某种原因,我认为以下可能有效:

enum MY_ENUM : unsigned __int64  
{  
    LARGE_VALUE = 0x1000000000000000,  
};

采纳答案by Ferruccio

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:

我认为 C++98 是不可能的。枚举的底层表示取决于编译器。在这种情况下,您最好使用:

const __int64 LARGE_VALUE = 0x1000000000000000L;

As of C++11, it is possible to use enum classes to specify the base type of the enum:

从 C++11 开始,可以使用枚举类来指定枚举的基本类型:

enum class MY_ENUM : unsigned __int64 {
    LARGE_VALUE = 0x1000000000000000ULL
};

In addition enum classes introduce a new name scope. So instead of referring to LARGE_VALUE, you would reference MY_ENUM::LARGE_VALUE.

此外,枚举类引入了一个新的名称范围。因此LARGE_VALUE,您将参考MY_ENUM::LARGE_VALUE.

回答by Leon Timmermans

C++11 supports this, using this syntax:

C++11 支持这一点,使用以下语法:

enum class Enum2 : __int64 {Val1, Val2, val3};

回答by mloskot

The current draft of so called C++0x, it is n3092says in 7.2 Enumeration declarations, paragraph 6:

所谓的C++0x的当前草案,它是n30927.2 Enumeration declarations,第 6 段中说:

It is implementation-defined which integral type is used as the underlying type except that the underlying type shall not be larger than int unless the value of an enumerator cannot fit in an int or unsigned int.

使用哪种整数类型作为底层类型是实现定义的,除非底层类型不应大于 int,除非枚举数的值不能适合 int 或 unsigned int。

The same paragraph also says:

同一段还说:

If no integral type can represent all the enumerator values, the enumeration is ill-formed.

如果没有整数类型可以表示所有枚举器值,则枚举格式错误。

My interpretation of the part unless the value of an enumerator cannot fit in an int or unsigned intis that it's perfectly valid and safe to initialise enumerator with 64-bit integer value as long as there is 64-bit integer type provided in a particular C++ implementation.

除非枚举器的值不能放入 int 或 unsigned int中,否则我对这部分的解释 是,只要在特定 C++ 中提供了 64 位整数类型,用 64 位整数值初始化枚举器是完全有效和安全的执行。

For example:

例如:

enum MyEnum
{
    Undefined = 0xffffffffffffffffULL
};

回答by MSalters

The answers refering to __int64miss the problem. The enum isvalid in all C++ compilers that have a true 64 bit integral type, i.e. any C++11 compiler, or C++03 compilers with appropriate extensions. Extensions to C++03 like __int64work differently across compilers, including its suitability as a base type for enums.

答案是指__int64错过问题。枚举在具有真正的64位整数类型,即,任何C ++编译器11,或C ++编译器03用适当的扩展名的所有C ++编译器有效。C++03 的扩展__int64在编译器之间的工作方式不同,包括它作为枚举的基本类型的适用性。

回答by INS

If the compiler doesn't support 64 bit enums by compilation flags or any other means I think there is no solution to this one.

如果编译器不通过编译标志或任何其他方式支持 64 位枚举,我认为没有解决方案。

You could create something like in your sample something like:

你可以在你的样本中创建类似的东西:

namespace MyNamespace {
const uint64 LARGE_VALUE = 0x1000000000000000;
};

and using it just like an enum using

并像使用枚举一样使用它

MyNamespace::LARGE_VALUE 

or

或者

using MyNamespace;
....
val = LARGE_VALUE;

回答by ugasoft

your snipplet of code is not c++ standard:

您的代码片段不是 C++ 标准:

enum MY_ENUM : unsigned __int64

枚举 MY_ENUM : 无符号 __int64

does not make sense.

没有意义。

use const __int64 instead, as Torlack suggests

使用 const __int64 代替,正如 Torlack 建议的那样

回答by Torlack

Since you are working in C++, another alternative might be

由于您使用的是 C++,因此另一种选择可能是

const __int64 LARVE_VALUE = ...

This can be specified in an H file.

这可以在 H 文件中指定。

回答by human.js

Enum type is normally determined by the data type of the first enum initializer. If the value should exceed the range for that integral datatype then c++ compiler will make sure it fits in by using a larger integral data type.If compiler finds that it does not belong to any of the integral data type then compiler will throw error. Ref: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1905.pdf
Edit: However this is purely depended on machine architecture

枚举类型通常由第一个枚举初始值设定项的数据类型确定。如果该值应超出该整数数据类型的范围,则 C++ 编译器将通过使用更大的整数数据类型来确保它适合。如果编译器发现它不属于任何整数数据类型,则编译器将抛出错误。参考:http: //www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1905.pdf
编辑:但这纯粹取决于机器架构

回答by Doug T.

An enum in C++ can be any integral type. You can, for example, have an enum of chars. IE:

C++ 中的枚举可以是任何整数类型。例如,您可以有一个字符枚举。IE:

enum MY_ENUM
{
   CHAR_VALUE = 'c',
};

I would assumethis includes __int64. Try just

认为这包括 __int64。试试吧

enum MY_ENUM
{
   LARGE_VALUE = 0x1000000000000000,
};

According to my commenter, sixlettervariables, in C the base type will be an int always, while in C++ the base type is whatever is large enough to fit the largest included value. So both enums above should work.

根据我的评论者,sixlettervariables,在 C 中,基类型将始终是 int,而在 C++ 中,基类型是足够大以适合最大包含值的任何类型。所以上面的两个枚举都应该工作。

回答by Doug T.

In MSVC++ you can do this:

在 MSVC++ 中,您可以这样做:

enum MYLONGLONGENUM:__int64 { BIG_KEY=0x3034303232303330, ... };

枚举 MYLONGLONGENUM:__int64 { BIG_KEY=0x3034303232303330, ... };