473,320 Members | 1,988 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,320 software developers and data experts.

Some problems about enumerations.

Today when I read TC++PL(se) section 4.8 Enumerations, I get some
problems.

1. In an enumeration, why the value of some enumerators can be the
same, for instance:
enum e1{a = 0, b = 0, c = 0};
we can't distinguish a, b and c by value, when they are used in
switch statement, the compiler will complain that they have the same
value. why not the standard forbid it, does it have some special
situation that have the same value is useful?

2. In Stroustrup's TC++PL(se) section 4.8, he has written that "The
notion of a range of values for an enumeration differs from the
enumeration notion in the Pascal family of languages. However, bit-
manipulation examples that require values outside the set of
enumerators to be well-defined have a long history in C and C++."
Can you give me a bit-manipulation example he mentioned here?

3. Also in TC++PL(se) section 4.8, he has written that "The sizeof an
enumeration is the sizeof some integral type that can hold its range
and not larger than sizeof (int), unless an enumerator cannot be
represented as an int or as an unsigned int".
If there is an enmerator which can't be represented as an int or as
an unsinged int , the sizeof the enumeration will be sizeof(long)?

Thanks for explaining it.

Regards.

Jul 1 '07 #1
1 1455
Wayne Shu wrote:
Today when I read TC++PL(se) section 4.8 Enumerations, I get some
problems.

1. In an enumeration, why the value of some enumerators can be the
same, for instance:
enum e1{a = 0, b = 0, c = 0};
we can't distinguish a, b and c by value, when they are used in
switch statement, the compiler will complain that they have the same
value. why not the standard forbid it, does it have some special
situation that have the same value is useful?
You could not prevent this anyway:

enum e1 { 0 };
e1 const a = 0;
e1 const b = 0;
e1 const c = 0;

If a programmer has a use for multiple names refering to the same constant
value, so be it. Why should the language interfere?

As for a special use case, some people make it a habit to always include
min_value, and max_value markers in their enums:

enum my_enum { min_value = 0, a = 0, b = 1, c = 2, max_value = 2 };

This way, templates can retrieve the boundaries of the enumerated values.
However, the names min_value and max_value are probably unrelated to the
problem domain, whence you need alternative proper names for those values,
too.

2. In Stroustrup's TC++PL(se) section 4.8, he has written that "The
notion of a range of values for an enumeration differs from the
enumeration notion in the Pascal family of languages. However, bit-
manipulation examples that require values outside the set of,
enumerators to be well-defined have a long history in C and C++."
Can you give me a bit-manipulation example he mentioned here?
enum flags { read = 1, write = 2 };

file = open_file ( read | write );

3. Also in TC++PL(se) section 4.8, he has written that "The sizeof an
enumeration is the sizeof some integral type that can hold its range
and not larger than sizeof (int), unless an enumerator cannot be
represented as an int or as an unsigned int".
If there is an enmerator which can't be represented as an int or as
an unsinged int , the sizeof the enumeration will be sizeof(long)?
Provided, long can hold it, yes.
Best

Kai-Uwe Bux
Jul 1 '07 #2

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

Similar topics

1
by: Joyce | last post by:
In my schema I have 2 enumerations, let's say, country description and country code, and I want to use them so I can map each country description to its precise country code (and no other). So far...
0
by: Plinkerton | last post by:
I'm making an Base Class that will be inherited. In my base class, I have a public enumeration that defines a list of things I want my class to be able to do. I use it for Method input...
0
by: Richard Taylor | last post by:
User-Agent: OSXnews 2.07 Xref: number1.nntp.dca.giganews.com comp.lang.python:437315 Hi I am trying to use py2app (http://undefined.org/python/) to package a gnome-python application...
5
by: Seamus M | last post by:
I can't find any info on enumerations in the PHP manual, so I assume there is no built in way to create them. Can anyone tell me the best way to build a simple enumeration, such as: Enum...
2
by: Glenn | last post by:
I've been trying to generate enumerations with specific integer values using an XSD and xsd.exe. e.g public enum AccessSecurity { Normal = 1, Restricted = 100001 }
1
by: Oleg Ogurok | last post by:
Hi all, I've added a new DataSet (xsd file) to my project in VS.NET 2003. There I create a simple type as an enumeration of values. <xs:simpleType name="MyCustomType"> <xs:restriction...
4
by: ChrisB | last post by:
Hello: I will be creating 50+ enumerations related to a large number of classes that span a number of namespaces. I was wondering if there are any "best practices" when defining enumerations. ...
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...
77
by: Ben Finney | last post by:
Howdy all, PEP 354: Enumerations in Python has been accepted as a draft PEP. The current version can be viewed online: <URL:http://www.python.org/peps/pep-0354.html> Here is the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
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...

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.