enum { A,B, }

S

Siemel Naran

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?
 
S

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.
 
A

Andrey Tarasevich

Siemel said:
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.
 
S

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)
 
G

Gianni Mariani

falcon said:
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.
 
M

Michael Etscheid

Gianni said:
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.
 
C

Chris Theis

falcon said:
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
 
S

Siemel Naran

Michael Etscheid said:
Gianni Mariani schrieb:

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.
 
G

Greg Comeau

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.
 
G

Gianni Mariani

Michael said:
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 ",".
 

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

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,660
Latest member
vidotip479

Latest Threads

Top