Connecting Tech Pros Worldwide Help | Site Map

enum { A,B, }

Siemel Naran
Guest
 
Posts: n/a
#1: Jul 23 '05
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?


Sep
Guest
 
Posts: n/a
#2: Jul 23 '05

re: enum { A,B, }


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.

Andrey Tarasevich
Guest
 
Posts: n/a
#3: Jul 23 '05

re: enum { A,B, }


Siemel Naran wrote:[color=blue]
> 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?
> ...[/color]

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


falcon
Guest
 
Posts: n/a
#4: Jul 23 '05

re: enum { A,B, }


It works in my gcc 2.95 c++ compiler.

Sep
Guest
 
Posts: n/a
#5: Jul 23 '05

re: enum { A,B, }


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)

Gianni Mariani
Guest
 
Posts: n/a
#6: Jul 23 '05

re: enum { A,B, }


falcon wrote:[color=blue]
> It works in my gcc 2.95 c++ compiler.
>[/color]

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.

Michael Etscheid
Guest
 
Posts: n/a
#7: Jul 23 '05

re: enum { A,B, }


Gianni Mariani schrieb:[color=blue]
> BTW - I think that a future revision of the C++ standard will likely
> allow this since it is now allowed in C99.[/color]

But when do you need this feature? IMHO it's completely superfluous.
Chris Theis
Guest
 
Posts: n/a
#8: Jul 23 '05

re: enum { A,B, }



"falcon" <shiju.cplusplus@gmail.com> wrote in message
news:1110703805.869083.94290@o13g2000cwo.googlegro ups.com...[color=blue]
> It works in my gcc 2.95 c++ compiler.[/color]

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


Siemel Naran
Guest
 
Posts: n/a
#9: Jul 23 '05

re: enum { A,B, }


"Michael Etscheid" <the.michael.e@gmail.com> wrote in message
news:d1217h$n50$00[color=blue]
> Gianni Mariani schrieb:[/color]
[color=blue][color=green]
> > BTW - I think that a future revision of the C++ standard will likely
> > allow this since it is now allowed in C99.[/color]
>
> But when do you need this feature? IMHO it's completely superfluous.[/color]

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.


Greg Comeau
Guest
 
Posts: n/a
#10: Jul 23 '05

re: enum { A,B, }


In article <d1217h$n50$00$1@news.t-online.com>,
Michael Etscheid <the.michael.e@gmail.com> wrote:[color=blue]
>Gianni Mariani schrieb:[color=green]
>> BTW - I think that a future revision of the C++ standard will likely
>> allow this since it is now allowed in C99.[/color]
>
>But when do you need this feature? IMHO it's completely superfluous.[/color]

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?
Gianni Mariani
Guest
 
Posts: n/a
#11: Jul 23 '05

re: enum { A,B, }


Michael Etscheid wrote:[color=blue]
> Gianni Mariani schrieb:
>[color=green]
>> BTW - I think that a future revision of the C++ standard will likely
>> allow this since it is now allowed in C99.[/color]
>
>
> But when do you need this feature? IMHO it's completely superfluous.[/color]

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

Closed Thread