Connecting Tech Pros Worldwide Help | Site Map

enum { A,B, }

  #1  
Old July 23rd, 2005, 02:58 AM
Siemel Naran
Guest
 
Posts: n/a
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?


  #2  
Old July 23rd, 2005, 02:58 AM
Sep
Guest
 
Posts: n/a

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.

  #3  
Old July 23rd, 2005, 02:58 AM
Andrey Tarasevich
Guest
 
Posts: n/a

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


  #4  
Old July 23rd, 2005, 02:59 AM
falcon
Guest
 
Posts: n/a

re: enum { A,B, }


It works in my gcc 2.95 c++ compiler.

  #5  
Old July 23rd, 2005, 02:59 AM
Sep
Guest
 
Posts: n/a

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)

  #6  
Old July 23rd, 2005, 02:59 AM
Gianni Mariani
Guest
 
Posts: n/a

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.

  #7  
Old July 23rd, 2005, 02:59 AM
Michael Etscheid
Guest
 
Posts: n/a

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.
  #8  
Old July 23rd, 2005, 02:59 AM
Chris Theis
Guest
 
Posts: n/a

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


  #9  
Old July 23rd, 2005, 02:59 AM
Siemel Naran
Guest
 
Posts: n/a

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.


  #10  
Old July 23rd, 2005, 02:59 AM
Greg Comeau
Guest
 
Posts: n/a

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?
  #11  
Old July 23rd, 2005, 03:00 AM
Gianni Mariani
Guest
 
Posts: n/a

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Typesafe Enum Pattern Steven Nagy answers 34 October 4th, 2006 11:45 PM
Problems Compiling enum.h from ENUM++ Randy answers 1 January 7th, 2006 11:05 PM
How to get Enum type using Enum name? Don answers 13 November 21st, 2005 03:09 PM
Why do enum values req explicit conversions Michael C answers 31 November 17th, 2005 01:31 AM
Rationale behind int to enum casts Andreas Huber answers 21 November 15th, 2005 04:47 PM