64 bit enum in C++?

2019-01-18 03:09发布

问题:

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,  
};

回答1:

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;

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
};

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



回答2:

C++11 supports this, using this syntax:

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


回答3:

The current draft of so called C++0x, it is n3092 says in 7.2 Enumeration declarations, paragraph 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.

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 int is 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.

For example:

enum MyEnum
{
    Undefined = 0xffffffffffffffffULL
};


回答4:

The answers refering to __int64 miss the problem. The enum is valid 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 __int64 work differently across compilers, including its suitability as a base type for enums.



回答5:

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.

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;


回答6:

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

const __int64 LARVE_VALUE = ...

This can be specified in an H file.



回答7:

your snipplet of code is not c++ standard:

enum MY_ENUM : unsigned __int64

does not make sense.

use const __int64 instead, as Torlack suggests



回答8:

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



回答9:

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

enum MY_ENUM
{
   CHAR_VALUE = 'c',
};

I would assume this includes __int64. Try just

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.



回答10:

In MSVC++ you can do this:

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



标签: c++ enums 64bit