Sign in to follow this  

Max number of enum values

This topic is 4197 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 :)

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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;
}
}


Share this post


Link to post
Share on other sites

This topic is 4197 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this