Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

trailing commas at the end of enumeration

1 view
Skip to first unread message

Jan Sneeuwman

unread,
Apr 20, 2008, 5:07:10 PM4/20/08
to
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

Andrey Tarasevich

unread,
Apr 20, 2008, 5:48:12 PM4/20/08
to
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

Ross A. Finlayson

unread,
Apr 20, 2008, 8:08:20 PM4/20/08
to

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/wg21/docs/cwg_defects.html#518


No, they're not.

Ross F.

Chris Thomasson

unread,
Apr 21, 2008, 12:20:27 AM4/21/08
to
"Jan Sneeuwman" <jan.sn...@gmail.com> wrote in message
news:b9WdnftebcgtLJbV...@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.lang.c/msg/eb5e6094ab2bc75e

James Kanze

unread,
Apr 21, 2008, 5:40:09 AM4/21/08
to
On Apr 20, 11:07 pm, Jan Sneeuwman <jan.sneeuw...@gmail.com> wrote:

> 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:james...@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

jan.sn...@gmail.com

unread,
Apr 21, 2008, 7:45:37 AM4/21/08
to
On Apr 21, 1:40 pm, James Kanze <james.ka...@gmail.com> wrote:
> [...] 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

jan.sn...@gmail.com

unread,
Apr 21, 2008, 7:44:59 AM4/21/08
to
On Apr 21, 1:40 pm, James Kanze <james.ka...@gmail.com> wrote:
> In C code, "gcc -std=c99 -pedantic" doesn't complain.
No, C++ code, of course. I meant "g++ -Wall -pedantic".

Jan

0 new messages