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
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
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.
Check this out:
http://groups.google.com/group/comp.lang.c/msg/eb5e6094ab2bc75e
> 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
Jan