473,385 Members | 1,940 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

C++0x Enumeration question

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.
};
Jun 27 '08 #1
8 1959
Sam
rc******@gmail.com writes:
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.
};
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?

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEABECAAYFAkhhhLcACgkQx9p3GYHlUOJeNQCfYYtxy8Cl4k +et9X5UKHF2GI+
MnQAnRi51EuVgVZ8FL+mfs3rfmbx0hTH
=wkkl
-----END PGP SIGNATURE-----

Jun 27 '08 #2
On Jun 24, 6:35*pm, Sam <s...@email-scan.comwrote:
rcdai...@gmail.com writes:
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.
};

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_FOOSfoo_bitset;
Jun 27 '08 #3
Sam
rc******@gmail.com writes:
On Jun 24, 6:35Â*pm, Sam <s...@email-scan.comwrote:
>rcdai...@gmail.com writes:
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.
};

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_FOOSfoo_bitset;
Except that -- as I pointed out -- this will fail miserably if the enum is
noncontinuous:

enum Foo {
FOO=0,
BAR=100
}

std::bitset<sizeof(Foo)foo_bitset;

sizeof(Foo) is two, since the num contains two values. Boom.

Or, if the enum contains duplicate values, things will even be more
exciting.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEABECAAYFAkhhlBUACgkQx9p3GYHlUOL09ACeNuUbHJrtJN XPeyjzTulMRCRi
0sYAn3kCKT+tIbfGoSWh3+bLLnGYbKcq
=Rh2Y
-----END PGP SIGNATURE-----

Jun 27 '08 #4
On Jun 25, 12:46 am, "rcdai...@gmail.com" <rcdai...@gmail.comwrote:
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.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 27 '08 #5
rc******@gmail.com wrote:
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).
Jun 27 '08 #6
rc******@gmail.com wrote:
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
Jun 27 '08 #7
Sam wrote:
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?
Jun 27 '08 #8
In article <vP*************@read4.inet.fi>, no****@thanks.invalid
says...
Sam wrote:
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?
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.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jun 27 '08 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Justin Wright | last post by:
I know that I can set up an enumeration as follows ( just typed in quick so may have syntax errors ): <xsd:simpleType name="colors"> <xsd:restriction base="xsd:string"> <xsd:enumeration...
14
by: Ioannis Vranos | last post by:
I would like to see your views on these. C++98 is already a large language since it supports 4 paradigms and each one is supported well, with optimal space and time efficiency. And this is...
3
by: Sampson | last post by:
I have a question about enumeration and how to populate them during runtime. I am using vb.net but will happily take any advice in c# as well. Here is an example to help illustrate what I am...
27
by: Ben Finney | last post by:
Antoon Pardon wrote: > I just downloaded your enum module for python > and played a bit with it. IMO some of the behaviour makes it less > usefull. Feedback is appreciated. I'm hoping to...
3
by: aarklon | last post by:
Hi all, in the article http://www.c-faq.com/struct/enumvsdefine.html it is written as Some advantages of enumerations are that the numeric values are automatically assigned, that a debugger...
2
by: LewGun | last post by:
at the end of last year Herb Sutter told us that "C++ 0x has been fixed", now GCC4.3 had been released, the compiler were known as "the C ++ new features' experimental unit",but it support to the...
6
by: Dmitriy V'jukov | last post by:
On 2 Á×Ç, 20:47, "Dmitriy V'jukov" <dvyu...@gmail.comwrote: Q: Can I use Relacy Race Detector to check my algo againts other that C ++0x memory models (x86, PPC, Java, CLI)? A Yes, you...
13
by: Dmitriy V'jukov | last post by:
Consider following Peterson's algorithm implementation from Wikipedia: http://en.wikipedia.org/wiki/Peterson%27s_algorithm flag = 0 flag = 0 turn = 0 P0: flag = 1 turn = 1...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.