enum { A,B, } 
July 23rd, 2005, 02:58 AM
| | | |
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? | 
July 23rd, 2005, 02:58 AM
| | | | 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. | 
July 23rd, 2005, 02:58 AM
| | | | 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 | 
July 23rd, 2005, 02:59 AM
| | | | re: enum { A,B, }
It works in my gcc 2.95 c++ compiler. | 
July 23rd, 2005, 02:59 AM
| | | | 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) | 
July 23rd, 2005, 02:59 AM
| | | | 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. | 
July 23rd, 2005, 02:59 AM
| | | | 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. | 
July 23rd, 2005, 02:59 AM
| | | | 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 | 
July 23rd, 2005, 02:59 AM
| | | | 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. | 
July 23rd, 2005, 02:59 AM
| | | | 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? | 
July 23rd, 2005, 03:00 AM
| | | | 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 ",". |  | | | | /bytes/about
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 225,702 network members.
|