tags:

views:

84

answers:

2

I'm using gcc, which implements enums as 32 bit integers on the architecture I have (don't know in general). If I try to assign an enum value too large, I get

warning: integer overflow in expression

Is there a way to make gcc use 64 bit integers as the underlying integer type? A gcc specific way is fine, although if there's a portable way, that's even better.

** Edit ** This is a related post: http://stackoverflow.com/questions/76624/64-bit-enum-in-c

Unlike that question, I'm also interested in gnu extensions.

A: 

one option: create a template class with a static const member of a specific type.

for example std::tr1::integral_constant, declared in c++/tr1/type_traits in the GNU distribution (at least, the one i'm using).

for an enum value: your declaration could matter (e.g., use U, L as appropriate)

Justin
A: 

The following works for me with -std=c++0x, but not with -std=c++98 though

enum EnumFoo {
    FooSomething = 0x123456789ULL
};

I tested this with

$ g++ --version
g++ (Ubuntu 4.4.3-4ubuntu5) 4.4.3
ArunSaha
Which version of gcc are you using? This doesn't seem to work for 4.3.4, I still get a 32bit enum.
pythonic metaphor
@pythonic metaphor: Updated my answer to address your question.
ArunSaha
`ULL` should tell your compiler to use C99's `unsigned long long` type, which has a minimum width of 64 bits (and is defined to be exactly 64 bits wide on all implementations I'm aware of.) However, if your compiler doesn't support `long long` (e.g. if you're in GCC and using *strict* C++98, which is what is meant by `-std=c++98`), `ULL` will either be seen as an error, or the extra `L` will be ignored and you'll get an `unsigned long` (which is often 32 bits wide.) I don't recall exactly which behaviour is standards-conformant offhand, but if it's giving you 32 bits, I suspect the latter.
Jonathan Grynspan