Max number of enum values

Started by
7 comments, last by NotAYakk 17 years, 10 months ago
Hi! How many enum values are permitted at most in a single enum ? For example look at:

enum eTestEnum : unsigned int
{
    eTest1,
    eTest2,
    ...
    eTestX
};
Since that enum values can be duplicated I think that in theory there is no maximum. But is there a maximum defined in the C++ standards ? (I tried to googlo that but didnt find anything. Visual C++ express doc doesn't talk about it either)
Advertisement
I don't know for a fact but I would guess it would be the maximum value of the underlying type. In your example that would be the max value of a UINT.
The C++ standard suggests (annex B), but does not mandate, that compilers support a minimum of 4096 enumeration constants in a single enumeration. Consult your compiler documentation for more information.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Ok thanks :) I'll stick with 4096 constants or use something else. I'm dealing with tool-generated code, thats why I'm concerned about how big an enum can grow.

I couldn't find anything in VC++ Express doc, so if someone happens to know something about it, I'd be glad to hear about it.
And Fruny btw, what is the C++ standard you refer to. Is it a link, a book ? Should it be a link you dont mind sharing, I think I could use it too :)
And you can't use defines in this generated code? (Perhaps not as elegant but at least has no limit)


Quote:Original post by Syranide
And you can't use defines in this generated code? (Perhaps not as elegant but at least has no limit)


Also has no type, from an function overloading point of view. However, a generated file:

typedef my_type unsigned int;const my_type a=0;const my_type b=1;etc


would solve both this and the 4096 limit problem.

[EDIT] Actually, I'm wondering now. A typedef does have a unique signature for funtion overloading, right?
Nope - a typedef creates an alias to an existing type, not a new type.
If you wanted to get really serious, you could:

A) Either do it by strings (can be slow)

B) Template the required functions, and generate small "key" classes, and use type_traits to identify things. This may beyond the scope of generated code.

C) Just use constants, const unsigned long long thing = 0; - there are over 1.8*10^9 unique possibilities (that's how many numbers there are!) and 64 bit-flags. Probably enough.
[ search: google ][ programming: msdn | boost | opengl ][ languages: nihongo ]
Or...

enum {value_one=7};enum {value_two=2};


The only advantage of multiple values in an enum is that they have a type. If you just want constants, they can be structured like the above.

If you want a type, you can do:

namespace generated_enum {  typedef enum {    value_1=7,    ...    value_4000=-2,  } gen_enum_1;  typedef enum {    ...  } gen_enum_2;  ...  // struct that defines the "unified" enum type:  struct gen_enum {    unsigned int value;    operator gen_enum_1() {return (gen_enum_1)value;}    gen_enum(gen_enum_1 v):value(v){};    ...  };  // support operator== between any two types in this namespace:  // (uses arguement-based lookup)  template<typename gen_enum_n, typename gen_enum_m>  bool operator==(gen_enum_n l_, gen_enum_m r_) {    gen_enum l=l_;    gen_enum r=r_;    return l.value==r.value;  }}

This topic is closed to new replies.

Advertisement