Will there be a feature in C++0x to facilitate getting the
number of items in an enumeration? For example, in C++03, you
have to do something like this:
enum Foo
{
FOO1 = 0,
FOO2,
FOO3,
NUM_FOOS // this will be 3, and we use this to determine how many
enums we have.
};
No. The real problem is that C++ doesn't have "enumerations",
in the classical sense of the word. It has a facility (using
the keyword enum) for creating new integral types and symbolic
constants for some (but not necessarily all) of the values of
those types. Thus, for example, what should such a facility
say to something like:
enum _Ios_Fmtflags
{
_S_boolalpha = 1L << 0,
_S_dec = 1L << 1,
_S_fixed = 1L << 2,
_S_hex = 1L << 3,
_S_internal = 1L << 4,
_S_left = 1L << 5,
_S_oct = 1L << 6,
_S_right = 1L << 7,
_S_scientific = 1L << 8,
_S_showbase = 1L << 9,
_S_showpoint = 1L << 10,
_S_showpos = 1L << 11,
_S_skipws = 1L << 12,
_S_unitbuf = 1L << 13,
_S_uppercase = 1L << 14,
_S_adjustfield = _S_left | _S_right | _S_internal,
_S_basefield = _S_dec | _S_oct | _S_hex,
_S_floatfield = _S_scientific | _S_fixed,
_S_ios_fmtflags_end = 1L << 16
};
(This is actual code from the g++ standard library, which
explains the somewhat strange naming convensions.)
I have a small utility program which I use which will generate
various information about enums---it was originally developed to
generate an enum name to value mapping, but has options for
additional functionality. One of the options will generate
increment and decrement operators for the enum, along with an
specialization of std::numeric_limits (whose member function
max() is probably what you are looking for) and iterators
accessing all of the enum values; this option will be rejected,
however, if any of the values in the enum have an assigned
value. (Similarly, the option to generate the | and & operators
will be rejected unless all of the enum values are given
explicitly.)