473,472 Members | 1,761 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Is 'enum Colour : int { red, green, blue };' legal?

I'm a bit confused here, since the help in M$ Visual Studio 2005 states the
syntax for enum as:

enum [tag] [: type] {enum-list} [declarator];

So, it would be possible to write:

enum Protocol_t : unsigned char { ICMP = 1, TCP = 6, UDP = 17 };
struct IPHeader {
// ...
Protocol_t Protocol;
// ...
};

It sounds like an M$ specific extension to me, but it's not listed as such
in VS2005's help section "Microsoft Extensions to C and C++". On the other
hand, it's not included in Schildt's "C++: The Complete Reference" (4th ed,
2003).

cul8r
/Jörn
Jun 5 '06 #1
13 2166
Jorn Ronnow wrote:
I'm a bit confused here, since the help in M$ Visual Studio 2005 states the
syntax for enum as:

enum [tag] [: type] {enum-list} [declarator];
The [: type] part is an extension. The C++ standard states

7.2§1 [...] enum identifier(opt) { enumerator-list(opt) }
So, it would be possible to write:

enum Protocol_t : unsigned char { ICMP = 1, TCP = 6, UDP = 17 };
struct IPHeader {
// ...
Protocol_t Protocol;
// ...
};

It sounds like an M$ specific extension to me, but it's not listed as such
in VS2005's help section "Microsoft Extensions to C and C++".
That's unfortunate, really. I suggest you notify them, even if it may
be to no avail.
On the other
hand, it's not included in Schildt's "C++: The Complete Reference" (4th ed,
2003).


You may read http://tinyurl.com/jyomc for information on this book.
Jonathan

Jun 5 '06 #2
Jorn Ronnow wrote:
I'm a bit confused here, since the help in M$ Visual Studio 2005
states the syntax for enum as:

enum [tag] [: type] {enum-list} [declarator];

So, it would be possible to write:

enum Protocol_t : unsigned char { ICMP = 1, TCP = 6, UDP = 17 };
struct IPHeader {
// ...
Protocol_t Protocol;
// ...
};

It sounds like an M$ specific extension to me, but it's not listed as
such in VS2005's help section "Microsoft Extensions to C and C++". On
the other hand, it's not included in Schildt's "C++: The Complete
Reference" (4th ed, 2003).


First and foremost, good that you asked here. At least knowledgeable
people will read and answer. AFAIK, the "[: type]" is non-standard.

Second, although it seems that in this case Schildt shows agreement
with what's considered correct in c.l.c++, in many cases Schildt's books
are not recommended, see www.accu.org. You've been warned.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 5 '06 #3

"Victor Bazarov" <v.********@comAcast.net> skrev i meddelandet
news:e6**********@news.datemas.de...
Jorn Ronnow wrote:
I'm a bit confused here, since the help in M$ Visual Studio 2005
states the syntax for enum as:

enum [tag] [: type] {enum-list} [declarator];

So, it would be possible to write:

enum Protocol_t : unsigned char { ICMP = 1, TCP = 6, UDP = 17 };
struct IPHeader {
// ...
Protocol_t Protocol;
// ...
};

It sounds like an M$ specific extension to me, but it's not listed
as
such in VS2005's help section "Microsoft Extensions to C and C++".
On
the other hand, it's not included in Schildt's "C++: The Complete
Reference" (4th ed, 2003).


First and foremost, good that you asked here. At least
knowledgeable
people will read and answer. AFAIK, the "[: type]" is non-standard.


But it is *a* standard, ECMA-372 The C++/CLI language

http://www.ecma-international.org/pu...s/Ecma-372.htm
Confusing, isn't it? :-)

Bo Persson
Jun 5 '06 #4
Bo Persson wrote:
But it is *a* standard, ECMA-372 The C++/CLI language

http://www.ecma-international.org/pu...s/Ecma-372.htm

Confusing, isn't it? :-)


A web site with .org on it does not a Standard (or a non-profit) make!

--
Phlip
Jun 5 '06 #5
Bo Persson wrote:
"Victor Bazarov" <v.********@comAcast.net> skrev i meddelandet
news:e6**********@news.datemas.de...
Jorn Ronnow wrote:
I'm a bit confused here, since the help in M$ Visual Studio 2005
states the syntax for enum as:

enum [tag] [: type] {enum-list} [declarator];

So, it would be possible to write:

enum Protocol_t : unsigned char { ICMP = 1, TCP = 6, UDP = 17 };
struct IPHeader {
// ...
Protocol_t Protocol;
// ...
};

It sounds like an M$ specific extension to me, but it's not listed
as
such in VS2005's help section "Microsoft Extensions to C and C++".
On
the other hand, it's not included in Schildt's "C++: The Complete
Reference" (4th ed, 2003).


First and foremost, good that you asked here. At least
knowledgeable
people will read and answer. AFAIK, the "[: type]" is non-standard.


But it is *a* standard, ECMA-372 The C++/CLI language

http://www.ecma-international.org/pu...s/Ecma-372.htm
Confusing, isn't it? :-)


The only Standard relevant _here_ is ISO/IEC 14992:2003. *It* makes
no reference to the one you named ("ECMA-372", whatever that is). No
confusion here, AFAICT.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 5 '06 #6
Victor Bazarov wrote:
The only Standard relevant _here_ is ISO/IEC 14992:2003. *It* makes
no reference to the one you named ("ECMA-372", whatever that is). No
confusion here, AFAICT.


I really have to upgrade I am still on ISO/IEC 14882:2003 :)

Jun 5 '06 #7
"Jonathan Mcdougall" wrote in message

It sounds like an M$ specific extension to me, but it's not listed as such in VS2005's help section "Microsoft Extensions to C and C++".
That's unfortunate, really. I suggest you notify them, even if it may
be to no avail.


It seems I'm better off trusting my instincts than the M$ list of
extensions. The mere presence of extensions is a bit of a perversion, IMHO,
but that's another debate.
On the other
hand, it's not included in Schildt's "C++: The Complete Reference" (4th ed, 2003).

You may read http://tinyurl.com/jyomc for information on this book.


That saved me from wasting my time, thanks!

cul8r
/Jörn
Jun 5 '06 #8
Jorn Ronnow wrote:
The mere presence of extensions is a bit of a perversion, IMHO,
but that's another debate.


Sometimes an extension intends to lead developers into a trap from which
they cannot return to a competitor's tools.

However, always remember these extensions are not free to add, and the
enum [:type] extension looks too feeble to return its value by trapping
programmers.

One big problem with the Standard enum system is it's only
defined as having "at least enough bits" to hold all its values. This
makes it worse than useless in every situation where you need an enum to
fit and pad reliably. The [:type] extension makes enums reliable enough to
compete with our favorite keyword, #define, in more situations...

Please discuss union-ing an enum with a type of known size here ->[ ].

Please write legibly!

--
Phlip

Jun 5 '06 #9
phlip wrote:
Jorn Ronnow wrote:
The mere presence of extensions is a bit of a perversion, IMHO,
but that's another debate.
Sometimes an extension intends to lead developers into a trap from which
they cannot return to a competitor's tools.

However, always remember these extensions are not free to add, and the
enum [:type] extension looks too feeble to return its value by trapping
programmers.

One big problem with the Standard enum system is it's only
defined as having "at least enough bits" to hold all its values. This
makes it worse than useless in every situation where you need an enum to
fit and pad reliably.


Actually, I never needed an enum to "fit and pad reliably". Btw: according
to what you say, all the built-in types are also worse than useless,
because they all just have "at least enough bits" to support a defined
minimum range. Adding that [:type] thing buys you nothing, because you are
still relying on assumtions about the sizes of those types.
The [:type] extension makes enums reliable enough to
compete with our favorite keyword, #define, in more situations...
With #define, you don't define variables.
Please discuss union-ing an enum with a type of known size here ->[ ].


I guess you're joking?
Jun 6 '06 #10
Jorn Ronnow wrote:
It sounds like an M$ specific extension to me, but it's not listed as such
in VS2005's help section "Microsoft Extensions to C and C++".
That's what you get when you get your language definition from the
vendor documentation that came with your compiler.

C++ has an ANSI standard that you can download quite cheaply. (Actually
if you Google around, you can easily find a free PDF copy).
On the other hand, it's not included in Schildt's "C++: The Complete Reference"
(4th ed, 2003).


Hahaha. Google for "bullschildt".

Jun 6 '06 #11
Kaz Kylheku wrote:
On the other hand, it's not included in Schildt's "C++: The Complete Reference"
(4th ed, 2003).


Hahaha. Google for "bullschildt".


This newsgroup's Special Need to bust on H. Schildt even extends to when
he correctly omits a non-Standard feature.

--
Phlip

Jun 6 '06 #12
Rolf Magnus wrote:
Actually, I never needed an enum to "fit and pad reliably". Btw: according
to what you say, all the built-in types are also worse than useless,
because they all just have "at least enough bits" to support a defined
minimum range. Adding that [:type] thing buys you nothing, because you are
still relying on assumtions about the sizes of those types.


Built-in types can't change their bit requirements when you put one
more of their values into use.

--
Phlip
Jun 6 '06 #13

"Rolf Magnus" <ra******@t-online.de> skrev i meddelandet
news:e6*************@news.t-online.com...
phlip wrote:
Jorn Ronnow wrote:
The mere presence of extensions is a bit of a perversion, IMHO,
but that's another debate.


Sometimes an extension intends to lead developers into a trap from
which
they cannot return to a competitor's tools.

However, always remember these extensions are not free to add, and
the
enum [:type] extension looks too feeble to return its value by
trapping
programmers.

One big problem with the Standard enum system is it's only
defined as having "at least enough bits" to hold all its values.
This
makes it worse than useless in every situation where you need an
enum to
fit and pad reliably.


Actually, I never needed an enum to "fit and pad reliably". Btw:
according
to what you say, all the built-in types are also worse than useless,
because they all just have "at least enough bits" to support a
defined
minimum range. Adding that [:type] thing buys you nothing, because
you are
still relying on assumtions about the sizes of those types.


The [:type] works for the other C++ language, because they have
defined the sizes for the integer types as well. :-)
In real C++ you can set the "enough bits", by adding a dummy element
to the enum

enum { one, two, three, enough_bits = MAX_INT };
That works just as well (or just as bad) as

enum:int {one, two, three};
Bo Persson
Jun 6 '06 #14

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

Similar topics

4
by: Mattias Brändström | last post by:
Hi! Suppose I want to do some thing like this: enum X { A, B, C, D }; int main() { X a1 = { A, B, C, D }; int size_a = sizeof(a1) / sizeof(X);
4
by: slot | last post by:
Is it possible to compare "enum" types to basic types like "unsigned short" type at all? Thanks!
6
by: James Brown | last post by:
Hi, I have the following enum declared: enum TOKEN { TOK_ID = 1000, TOK_NUMBER, TOK_STRING, /*lots more here*/ }; What I am trying to do is _also_ represent ASCII values 0-127 as TOKENs...
18
by: Nebula | last post by:
Consider enum Side {Back,Front,Top,Bottom}; enum Side a; Now, why is a = 124; legal (well it really is an integer, but still, checking could be performed..) ? Wouldn't enums be more useful if...
5
by: Francois Grieu | last post by:
Can an enum be used as an array size? In other word, is this legal? enum {n=1}; int a; int main(void){return a;} TIA
21
by: Andreas Huber | last post by:
Hi there Spending half an hour searching through the archive I haven't found a rationale for the following behavior. using System; // note the missing Flags attribute enum Color {
1
by: Anonieko Ramos | last post by:
Answer: http://weblogs.asp.net/tims/archive/2004/04/02/106310.aspx by Tim Sneath I've come across the situation on a number of occasions when coding where I've wanted to convert from a string...
2
by: Alex Vinokur | last post by:
--- File foo.cpp --- class Foo { private: enum Eoo { aaa, bbb, ccc }; struct Soo { Eoo m_code; }; };
6
by: sethukr | last post by:
can we define enum within a structure?? for example, typedef enum {red,blue}color; struct s1{ color c; }obj;
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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.