Number of "constants" in enum [duplicate]
Possible Duplicate:
(How) can I count the items in an enum?
Is there a way to get the number of constants in enum?
For example:enum C{id,value};
And later this would return 开发者_运维百科2:
//pseudo code
get<C>::value
And also, is it possible to access those constants via [] optor? Like i.e.:
C[0] would return idUsually, you start at zero and the last member gives the size of the enum excluding it.
enum C { id = 0, value, size };
C::size
is the size of the enum. Is it possible to access those constants via subscript? No, it is unfortunately most assuredly not possible. However, in this case, you don't really want an enum- you just want a constant array.
A common idiom used for this is
enum C {
id,
value,
LAST_ENUM_C // or something similar.
};
but that assumes no gaps in the enum values here (i.e. no id = 3, value = 15
).
精彩评论