Is there a way to find the cardinality (size) of an enum in C++? -


Can someone write a function that gives the number of elements in an enum? For example, I have defined:

  enum E {x, y, z};  

Then F (E) returns 3.

no

If this happens, you will not see so much code like this:

  enum E {VALUE_BLAH, VALUE_OTHERBLAH, ... VALUE_FINALBLAH, VALUE_COUNT}  

Note that this code is also an indication for the solution (bad) - if you add the last "guard" element, and clearly do not tell the value of enum areas, the last "COUNT "Element will be the value you are looking for - this is because the enum calculation is zero-based:

  enum B {ONE, // in value = 0 two  
, in the value = 1 THREE, // in value = 2 COUNT // in value = 3 - without Een cardinality}

Comments

Popular posts from this blog

sql - dynamically varied number of conditions in the 'where' statement using LINQ -

asp.net mvc - Dynamically Generated Ajax.BeginForm -

Debug on symbian -