C++0x Enumeration question

R

rcdailey

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.
};
 
R

rcdailey

Given that enumerated values do not have to be continuous (or even unique),
what benefit do you see in knowing the number of enumerated values?

 application_pgp-signature_part
1KDownload

They're useful currently in C++03 in std::bitset objects:

std::bitset<NUM_FOOS> foo_bitset;
 
J

James Kanze

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.)
 
N

Noah Roberts

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.
};

I know of nothing that will be changing that. I don't know everything
that's going into the standard but I've been sort of following it with
interest. What you're talking about is simply an easy and common way to
do what you want to do. I wouldn't think it worth any language change
and there are a lot of other, much more important things that might not
make it in because of timing constraints (trying to get the std out this
decade).
 
M

Michael Oswald

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.
};

As others already said, an enum in C++ is not an enum like in Ada, where
you can do such things very easily. Since you can set the enum values
quite freely, this change would break compatibility with old code, so I
think you cannot expect that something like this will be added.


hth,
Michael
 
J

Juha Nieminen

Sam said:
Given that enumerated values do not have to be continuous (or even
unique), what benefit do you see in knowing the number of enumerated
values?

Btw, if an enumerated list is specified without any values (ie. like
enum Foo { a, b, c, d };), is it guaranteed that the first value will be
0 and the next ones will be consecutive positive integers?
 
J

Jerry Coffin

Btw, if an enumerated list is specified without any values (ie. like
enum Foo { a, b, c, d };), is it guaranteed that the first value will be
0 and the next ones will be consecutive positive integers?

Yes. ($7.2/1):

If the first enumerator has no initializer, the value
of the corresponding constant is zero. An enumerator-
definition without an initializer gives the enumerator
the value obtained by increasing the value of the
previous enumerator by one.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,814
Latest member
SpicetreeDigital

Latest Threads

Top