470,810 Members | 1,446 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,810 developers. It's quick & easy.

trailing commas at the end of enumeration

Hello,

I am working on a library in C++, and the library goes with a header file
with a trailing comma at the end of some enumeration. Like this:

enum Abc {
ENUM_ONE,
ENUM_TWO
ENUM_THREE,
#ifdef HAVE_SOMETHING
ENUM_FOUR,
#endif
ENUM_FIVE,
};

I believe this isn't very good to give away such a thing, because for
example
gcc with "-pedantic" flag barks at it with "error: comma at the end of
enumeration".
The library isn't mine, and the header file is generated, so this isn't
particularly
easy for me to make an edit.

I'm still not very sure if this is a bad style or not?
The C++ Standard (7.2) forbids such declaration:

enum-specifier:
enum identifier { enumerator-list }
enumerator-list:
enumerator-definition
enumerator-list , enumerator-definition
while C99 (6.7.2.2) clearly allows:

enum-specifier:
enum identifier { enumerator-list }
enum identifier { enumerator-list , }
enumerator-list:
enumerator
enumerator-list , enumerator

The question is: are those hanging commas ok or not?
Thanks!

Jan
Jun 27 '08 #1
6 8615
Jan Sneeuwman wrote:
The C++ Standard (7.2) forbids such declaration:
...
while C99 (6.7.2.2) clearly allows:
...
The question is: are those hanging commas ok or not?
You already answered your own question. Trailing comma in enum is illegal in
C89/90 and C++, but legal in C99. Which means that they are OK in C99 code only.

--
Best regards,
Andrey Tarasevich
Jun 27 '08 #2
Jan Sneeuwman wrote:
Hello,

I am working on a library in C++, and the library goes with a header file
with a trailing comma at the end of some enumeration. Like this:

enum Abc {
ENUM_ONE,
ENUM_TWO
ENUM_THREE,
#ifdef HAVE_SOMETHING
ENUM_FOUR,
#endif
ENUM_FIVE,
};

I believe this isn't very good to give away such a thing, because for
example
gcc with "-pedantic" flag barks at it with "error: comma at the end of
enumeration".
The library isn't mine, and the header file is generated, so this isn't
particularly
easy for me to make an edit.

I'm still not very sure if this is a bad style or not?
The C++ Standard (7.2) forbids such declaration:

enum-specifier:
enum identifier { enumerator-list }
enumerator-list:
enumerator-definition
enumerator-list , enumerator-definition
while C99 (6.7.2.2) clearly allows:

enum-specifier:
enum identifier { enumerator-list }
enum identifier { enumerator-list , }
enumerator-list:
enumerator
enumerator-list , enumerator

The question is: are those hanging commas ok or not?
Thanks!

Jan
In the generation of code, it is often inefficient to try and figure
whether to not emit the trailing comma. Sometimes there might be
emitted a range marker for the enum, which doesn't have a trailing
comma, so all the emitted enumerands (which seems to be a word) are
uniformly emitted, and the enumerator-list sequence doesn't end with a
comma.

http://www.nongnu.org/hcb/#enum-specifier
http://www.open-std.org/jtc1/sc22/wg...fects.html#518
No, they're not.

Ross F.
Jun 27 '08 #3
"Jan Sneeuwman" <ja***********@gmail.comwrote in message
news:b9******************************@giganews.com ...
Hello,

I am working on a library in C++, and the library goes with a header file
with a trailing comma at the end of some enumeration. Like this:

enum Abc {
ENUM_ONE,
ENUM_TWO
ENUM_THREE,
#ifdef HAVE_SOMETHING
ENUM_FOUR,
#endif
ENUM_FIVE,
};
[...]

Check this out:

http://groups.google.com/group/comp....5e6094ab2bc75e

Jun 27 '08 #4
On Apr 20, 11:07 pm, Jan Sneeuwman <jan.sneeuw...@gmail.comwrote:
I am working on a library in C++, and the library goes with a
header file with a trailing comma at the end of some
enumeration. Like this:
enum Abc {
ENUM_ONE,
ENUM_TWO
ENUM_THREE,
#ifdef HAVE_SOMETHING
ENUM_FOUR,
#endif
ENUM_FIVE,
};
I believe this isn't very good to give away such a thing,
because for example gcc with "-pedantic" flag barks at it with
"error: comma at the end of enumeration".
In C code, "gcc -std=c99 -pedantic" doesn't complain.
The library isn't mine, and the header file is generated, so
this isn't particularly easy for me to make an edit.
Then you may have to live with it, and not use the -pedantic
flag.
I'm still not very sure if this is a bad style or not? The
C++ Standard (7.2) forbids such declaration:
enum-specifier:
enum identifier { enumerator-list }
enumerator-list:
enumerator-definition
enumerator-list , enumerator-definition
So it's not a question of style; it's illegal.
while C99 (6.7.2.2) clearly allows:
enum-specifier:
enum identifier { enumerator-list }
enum identifier { enumerator-list , }
enumerator-list:
enumerator
enumerator-list , enumerator
The question is: are those hanging commas ok or not?
I believe that the orginal intent was always that they be legal.
Due to an oversight, there were illegal in C90, however, and C++
adopted the C90 definition of enums, so they're illegal in
C++03. The next version of C++ adopts C99 as its base, and they
will be legal.

In the meantime, however, while I might consider allowing them
for internal use, I certainly wouldn't deliver any code to
outside the firm which uses them.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 27 '08 #5
On Apr 21, 1:40 pm, James Kanze <james.ka...@gmail.comwrote:
[...] I certainly wouldn't deliver any code to
outside the firm which uses them.
Thank you all! I get it. The trailing commas won't be legal
until the new C++ standard is published. And the customer might
be disappointed (in case he'll use "-pedantic"),
and disappointed justly. Thank you!

Jan
Jun 27 '08 #6
On Apr 21, 1:40 pm, James Kanze <james.ka...@gmail.comwrote:
In C code, "gcc -std=c99 -pedantic" doesn't complain.
No, C++ code, of course. I meant "g++ -Wall -pedantic".

Jan
Jun 27 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

27 posts views Thread by Alberto Vera | last post: by
1 post views Thread by Justin Wright | last post: by
22 posts views Thread by ineedyourluvin1 | last post: by
4 posts views Thread by Marshal | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.