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 ca
C++11 supports this, using this syntax:
enum class Enum2 : __int64 {Val1, Val2, val3};
Since you are working in C++, another alternative might be
const __int64 LARVE_VALUE = ...
This can be specified in an H file.
your snipplet of code is not c++ standard:
enum MY_ENUM : unsigned __int64
does not make sense.
use const __int64 instead, as Torlack suggests
In MSVC++ you can do this:
enum MYLONGLONGENUM:__int64 { BIG_KEY=0x3034303232303330, ... };
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
};
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.