Connecting Tech Pros Worldwide Help | Site Map

enum { A,B, }

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 23rd, 2005, 01:58 AM
Siemel Naran
Guest
 
Posts: n/a
Default 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?



  #2  
Old July 23rd, 2005, 01:58 AM
Sep
Guest
 
Posts: n/a
Default 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, 01:58 AM
Andrey Tarasevich
Guest
 
Posts: n/a
Default 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, 01:59 AM
falcon
Guest
 
Posts: n/a
Default Re: enum { A,B, }

It works in my gcc 2.95 c++ compiler.

  #5  
Old July 23rd, 2005, 01:59 AM
Sep
Guest
 
Posts: n/a
Default 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, 01:59 AM
Gianni Mariani
Guest
 
Posts: n/a
Default 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, 01:59 AM
Michael Etscheid
Guest
 
Posts: n/a
Default 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, 01:59 AM
Chris Theis
Guest
 
Posts: n/a
Default 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, 01:59 AM
Siemel Naran
Guest
 
Posts: n/a
Default 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, 01:59 AM
Greg Comeau
Guest
 
Posts: n/a
Default 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, 02:00 AM
Gianni Mariani
Guest
 
Posts: n/a
Default 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 ",".

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.