tags:

views:

192

answers:

3
+6  Q: 

enum element limit

Is there a maximum number of allowable enum elements in C++?

(Question arose from answer to my previous question on defines)

+10  A: 

There isn't any specified maximum or minimum, it depends on your implementation. However, note that Annex B states:

— Enumeration constants in a single enumeration [4096].

As a recommendation. But this is strictly a recommendation, not a requirement.

GMan
Crap, I really need that 4294967297th member!!
Skurmedel
Or allow less because the Compiler crashes because it has not enough RAM to keep all these symbols in memory :-)
jdehaan
Of course, if the 4,294,967,297th enumerator is declared as `name = 0`, you could keep going, the enumerators just don't have unique values anymore :-P
James McNellis
@GMan I think you are confusing the value of an enum identifier with the number of possible enumerations identifiers. for example enum { a = 100000 }; has only one enumeration identifier which has the value 100000. So the standard says that an enum should support at least 4K identifiers.
anon
@Neil: Ah, you're right! Silly me.
GMan
@James: I thought `enum` elements have unique values otherwise you get a `Enum Syntax Error`?
0A0D
@Changeling: Nope, an `enum` is just a type with a list of constants, those constants can repeat. (Indeed, in the very question you linked us to in your post we see different identifiers with identical values.)
GMan
IS the 4086 a minimum requirement for the compiler to support?
Martin York
@GMan: I think that situation is slightly different.. If you do name = 0 and then down the line you say name = 0 again, then you get `Enum Syntax Error` by redefining the enum value.
0A0D
@Martin: Nope, there is no required minimum. 4096 is merely a recommendation. (Annex B starts by basically saying "We understand computers are finite in resources, so try your best. We recommend at least supporting these limits, but you don't have to.") I think this means a compiler must only support an empty `enum`, though such a compiler sucks. @Changeling: Nope, try it. I'm not even sure where you're getting that "Enum Syntax Error" error thing from; another language maybe? An `enum` is really just a type with a list of constants. This is completely valid: `enum foo { bar = 0, baz = 0, };`
GMan
@GMan: I guess I am not explaining it well. Do this `enum foo { bar = 0, bar = 1};` - You'll get an error
0A0D
@Changeling: Ah, yes. That's because you've got two identifiers with the same name. But that has nothing to do with the values of the identifiers, which is what @James was saying. Your example has nothing to do with what you said, namely: "I thought enum elements have unique values otherwise you get a Enum Syntax Error? ". Unique identifiers is not the same as unique values.
GMan
@Martin: __4096__! That's a power of two! How can you get that wrong?? Typo? `:)`
sbi
Does anyone have a real-world example where they had thousands of values for an enum? Maybe I'm living in a fantasy world, but I can't recall an example where I had more than a dozen or so. If it gets beyond that, I suspect this is not really an enum but a code lookup table.
Jay
@Jay: Machine states
0A0D
@GMan: Thanks!.
0A0D
@Jay: On a project we used enum to represent error codes. There was a lot of error codes then enum was then used to look up the locale specific string for the error.
Martin York
@GMan: Thanks..
0A0D
@Changeling: No probs.
GMan
@York: Okay, I'll buy that one. I've never had to internationalize an app -- advantage of living in the U.S. -- so I tend to hard-code my messages.
Jay
@Jay: The first time I, as a European, had to delve into Unicode and internationalization was when a company I worked for did a project for a U.S. company, which needed to deliver CJK translations of their app.
sbi
@sbi: I was thinking that Europeans probably do the most international apps because you have more languages per square mile. In the U.S. we have about as many people as in Europe and almost all speak English or Spanish.
Jay
@Jay: Which is exactly why I wrote about that episode. You aren't safe there, since one day your employer might decide to ship to international customers. Office suites, Adobe's CS, device drivers - many applications shipped internationally need to be translated. And once you think about CJK, 8bit is seriously and totally out of the question.
sbi
@sbi: Oh, I'm not saying I'm against internationalization or think it's a pointless thing to do or anything like that. 'Just saying I haven't had occasion to do that.
Jay
@Jay: I didn't understand you as being against i18n. It just felt that you seem glad to have avoided it so far, so I wanted to warn you. `:)`
sbi
@sbi: Actually, like many software-related things I've never done, I think it would be interesting to try it at least once. Of course, there are things where one try is enough. Like RPG.
Jay
+4  A: 

The language doesn't specify any such thing. However, compilers can have limits. You'd have to check your compiler docs for that.

T.E.D.
+1  A: 

In the case of C an enum is just a better scoped set of #defines. Whatever that means in detail from the standard C: an enum value is of a

type that is compatible with an implementation-defined one of the integral types.

My guess is that C++ has a similar definition and C++0x adds some typing possibility. All in one that would mean the amount you can have of them is theoritically limited by the underlying type (whatever it is? int most of the time, I suppose, the C standard is not clear enough regarding this). But before you can setup millions of symbols your compiler will crash or probably run out of memory.

jdehaan