enum element limit

2020-07-07 11:26发布

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

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

标签: c++ enums
3条回答
时光不老,我们不散
2楼-- · 2020-07-07 12:08

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.

查看更多
相关推荐>>
3楼-- · 2020-07-07 12:08

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

查看更多
手持菜刀,她持情操
4楼-- · 2020-07-07 12:30

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.

查看更多
登录 后发表回答