开发者

Number of "constants" in enum [duplicate]

This question already has answers here: Closed 11 years ago.

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 id


Usually, 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).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜