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