tags:

views:

2480

answers:

9

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

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.

Doug T.
@Doug T.: while ANSI C dictates that enumerations are the size of the 'int' data type, ISO C++ dictates that enumerations are of the size at least as large as required to represent all values.
sixlettervariables
+9  A: 

I don't think that's possible. The underlying representation of enums is up to the compiler. You are better off using:

const __int64 LARGE_VALUE = 0x1000000000000000L;
Ferruccio
This is what I resorted to in the end, but I was curious as to whether 64 bit enums are possible, even with a compiler specific extension.
Rob
+1  A: 

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

const __int64 LARVE_VALUE = ...

This can be specified in an H file.

Torlack
LARVE?‌‌‌‌‌‌‌‌‌
Behrooz
I cheated 9 chars with first try.
Behrooz
A: 

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;
Iulian Şerbănoiu
And loose type safety, though.
phresnel
A: 

your snipplet of code is not c++ standard:

enum MY_ENUM : unsigned __int64

does not make sense.

use const __int64 instead, as Torlack suggests

ugasoft
he has prolly seen it in another curly brace language (e.g. C# supports it) or in the upcoming C++ standard, where this will be allowed.
phresnel
+5  A: 

Currently C++ doesn't support this. C++0X will support this, using this syntax:

enum class Enum2 : __int64 {Val1, Val2, val3};
Leon Timmermans
I was close then. I must of read about the `: type` syntax somewhere.
Rob
note that `class` is still optional. If you want old style enum, but with a custom type, you can also `enum moo : long long {...}`
phresnel
+3  A: 

The answers refering to __int64 miss the problem. The enum is valid in all C++ compilers that have a true 64 bit integral type. It only fails in compilers which lack such an integral type - todays minimum is 32 bits(unsigned long). Now, C++0x will likely have long long, which means that all C++0x compilers will have at least one sufficiently large integral type, and thus can support 64 bits enums.

Extensions to C++03 like __int64 work differently across compilers, including its suitability as a base type for enums. That's what you get with non-standard code

MSalters
A: 

In MSVC++ you can do this:

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

better use int64-max than a magic wild guess.
phresnel
+1  A: 

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