8

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

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

Community
  • 1
  • 1
  • 7
    IMO, if you need to ask, you better review your design; but I think it could be related to the size of an "int". M. – Max Jul 12 '10 at 19:17
  • @Max: Is there a basis for thinking that? Specifying all named elements in one enum is better than spreading it across multiple files for maintability sake –  Jul 12 '10 at 19:26
  • What compiler are you using? The number of allowable values in an `enum` varies from implementation to implementation. – bta Jul 12 '10 at 19:40
  • 1
    @max: You forgot about __generated code__. An automatically generated state machine could have quite many states. – sbi Jul 12 '10 at 20:01
  • @Changeling: If you have a specific compiler, try it out. Have some script generate files with different numbers of enum values and compile these. See where the compiler chokes. Find the exact number by binary approximation. Then try a file of several different enums with that number of values, in case the value depends on the number of identifiers in scope, not in one enum. – sbi Jul 12 '10 at 20:08
  • @sbi: Good idea except it sometimes easier to ask the "cloud" :) –  Jul 12 '10 at 20:17
  • @Changeling: Yes, I generally consider it better to ask what the compiler __ought__ to do first, before interpreting what it seems to do. I didn't mean to criticize you asking here. I was just trying to pint out that, now, when this appears to be settled as a compiler-dependent limitation, checking it out might be a good idea. – sbi Jul 12 '10 at 20:24
  • @sbi: No problem. No offense taken. It is a good idea to try that too. –  Jul 12 '10 at 20:24

3 Answers3

12

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.

GManNickG
  • 494,350
  • 52
  • 494
  • 543
  • 4
    Crap, I really need that 4294967297th member!! – Skurmedel Jul 12 '10 at 19:26
  • Or allow less because the Compiler crashes because it has not enough RAM to keep all these symbols in memory :-) – jdehaan Jul 12 '10 at 19:30
  • 3
    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 Jul 12 '10 at 19:36
  • 1
    @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. –  Jul 12 '10 at 19:42
  • @James: I thought `enum` elements have unique values otherwise you get a `Enum Syntax Error`? –  Jul 12 '10 at 19:46
  • 1
    @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.) – GManNickG Jul 12 '10 at 19:48
  • IS the 4086 a minimum requirement for the compiler to support? – Martin York Jul 12 '10 at 19:53
  • @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. –  Jul 12 '10 at 19:53
  • @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, };` – GManNickG Jul 12 '10 at 19:57
  • @GMan: I guess I am not explaining it well. Do this `enum foo { bar = 0, bar = 1};` - You'll get an error –  Jul 12 '10 at 20:00
  • @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. – GManNickG Jul 12 '10 at 20:05
  • 2
    @Martin: __4096__! That's a power of two! How can you get that wrong?? Typo? `:)` – sbi Jul 12 '10 at 20:05
  • 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 Jul 12 '10 at 20:12
  • 1
    @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 Jul 12 '10 at 21:08
  • @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 Jul 13 '10 at 13:31
  • @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 Jul 17 '10 at 12:03
  • @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 Jul 17 '10 at 15:06
  • @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 Jul 17 '10 at 17:20
  • @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 Jul 18 '10 at 04:34
  • @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 Jul 18 '10 at 06:46
  • @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 Jul 18 '10 at 23:43
4

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.
  • 44,016
  • 10
  • 73
  • 134
1

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
  • 19,700
  • 6
  • 57
  • 97