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

enum { A,B, }

Hi. I notice that my compiler accepts

enum
{
A, // comment
B, // comment
};

Notice the trailing comma after B.

Is this legal per the ANSI C and C++ standards?
Jul 23 '05 #1
10 2252
Sep
No, it is a non-standard extension provided by your compiler. It is
illegal according to §7.2.1 (not explicitly, but it lists the only
acceptable forms of enumeration declaration allowed) of the ISO/IEC
C++-2003 standard.

Jul 23 '05 #2
Siemel Naran wrote:
Hi. I notice that my compiler accepts

enum
{
A, // comment
B, // comment
};

Notice the trailing comma after B.

Is this legal per the ANSI C and C++ standards?
...


It is legal in C99, but illegal in C89/90 and C++.

Note that in aggregate initializers the trailing comma is legal in C and
C++

int a[] = { 1, 2, 3, };

C99 simply "synchronized" the syntax of these two syntactically similar
constructs. Many compilers do the same, allowing the extra comma in enum
definitions even in C++ code.

--
Best regards,
Andrey Tarasevich
Jul 23 '05 #3
It works in my gcc 2.95 c++ compiler.

Jul 23 '05 #4
Sep
But no compiler is 100% standards-compliant, thus just because it works
in a given compiler does not mean it is acceptable according to the
standard. (Especially, nor does it mean it is acceptable as portable
code as some compilers may allow it as a non-standard extension and
others may not)

Jul 23 '05 #5
falcon wrote:
It works in my gcc 2.95 c++ compiler.


Comeau C/C++ 4.3.3 (Aug 6 2003 15:13:37) for ONLINE_EVALUATION_BETA1
Copyright 1988-2003 Comeau Computing. All rights reserved.
MODE:strict errors C++

"ComeauTest.c", line 4: error: trailing comma is nonstandard
B, // comment
^

1 error detected in the compilation of "ComeauTest.c".

It does not work on the Comeau compiler (in strict mode).

BTW - I think that a future revision of the C++ standard will likely
allow this since it is now allowed in C99.

Jul 23 '05 #6
Gianni Mariani schrieb:
BTW - I think that a future revision of the C++ standard will likely
allow this since it is now allowed in C99.


But when do you need this feature? IMHO it's completely superfluous.
Jul 23 '05 #7

"falcon" <sh*************@gmail.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
It works in my gcc 2.95 c++ compiler.


YouŽd be amazed what works with certain compilers and is still not legal.
Finding that something works with a compiler is not necessarily a proof that
the construct is legal. On one hande compilers provide extensions, on the
other hand they have their faults and/or are never 100% standard compliant.
You should test with a suite of different compilers and still take a look at
the specific section of the standard to be sure.

Cheers
Chris
Jul 23 '05 #8
"Michael Etscheid" <th***********@gmail.com> wrote in message
news:d1217h$n50$00
Gianni Mariani schrieb:

BTW - I think that a future revision of the C++ standard will likely
allow this since it is now allowed in C99.


But when do you need this feature? IMHO it's completely superfluous.


For me it happenned by accident when I moved enums from one place to
another.

Starting with

enum
{
A, // comment A
B, // comment B
C // comment C
};

Make C the first enum

enum
{
C // comment C
A, // comment A
B, // comment B
};

and I put a comma after C, but forgot to remove the one after B.

enum
{
C, // comment C
A, // comment A
B, // comment B
};

And it compiled on my computer.
Jul 23 '05 #9
In article <d1*************@news.t-online.com>,
Michael Etscheid <th***********@gmail.com> wrote:
Gianni Mariani schrieb:
BTW - I think that a future revision of the C++ standard will likely
allow this since it is now allowed in C99.


But when do you need this feature? IMHO it's completely superfluous.


Perhaps machine generated code.
Most it's just a syntactic remnant, and/but if it's going to
be allowed there it seems inconsistent to not allow it here.
--
Greg Comeau / Comeau for the Mac? Stay tuned.
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Jul 23 '05 #10
Michael Etscheid wrote:
Gianni Mariani schrieb:
BTW - I think that a future revision of the C++ standard will likely
allow this since it is now allowed in C99.

But when do you need this feature? IMHO it's completely superfluous.


Some macros I've written are much easier to write if the compiler
accepts the ",".

Jul 23 '05 #11

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

Similar topics

20
by: Glenn Venzke | last post by:
I'm writing a class with a method that will accept 1 of 3 items listed in an enum. Is it possible to pass the item name without the enum name in your calling statement? EXAMPLE: public enum...
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 {
31
by: Michael C | last post by:
If a class inherits from another class, say Form inherits from control, then I can assign the Form to a variable of type Control without needing an explicit conversion, eg Form1 f = new Form1();...
18
by: Visual Systems AB \(Martin Arvidsson\) | last post by:
Hi! I have created an enum list like this: enum myEnum : int { This = 2, That, NewVal = 10, LastItm
2
by: Dennis | last post by:
I have an enum as follows: Public Enum myData FirstData = 6 SecondData = 7 end enum Is there anyway that I can return the Enum names by their value, i.e., I want to input 6 into a function...
13
by: Don | last post by:
How do I get an Enum's type using only the Enum name? e.g. Dim enumType as System.Type Dim enumName as String = "MyEnum" enumType = ???(enumName)
10
by: Randy | last post by:
Hi, Can anyone point me to a complete, compilable example of Besser's ENUM++ mechanism? I downloaded it from CUJ and gave it a try but got errors just trying to compile the header enum.h. ...
1
by: Randy | last post by:
Hi, I downloaded and tried the ENUM++ code from CUJ http://www.cuj.com/documents/s=8470/cujboost0306besser/ but can't even get it to compile (see following). I have also downloaded and...
2
by: Randy | last post by:
Hi, I downloaded and tried the ENUM++ code from CUJ http://www.cuj.com/documents/s=8470/cujboost0306besser/ but can't even get it to compile (see following). I have also downloaded and...
34
by: Steven Nagy | last post by:
So I was needing some extra power from my enums and implemented the typesafe enum pattern. And it got me to thinking... why should I EVER use standard enums? There's now a nice little code...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.