I have read somewhere that the C++ standard does not allow something like enum an_enum { a, b, c, };, while later versions of C (I think from mid-90s) do allow such declarations with trailing commas. If C++ is supposed to have backwards compatibility with C, how come this feature is forbidden? Any special reason?
I also read that such trailing commas are actually good, so that just adds to the confusion.
C++03 (which is a fairly minor update of C++98) bases its C compatibility on C89 (also known as C90, depending on whether you’re ANSI or ISO). C89 doesn’t allow the trailing comma. C99 does allow it. C++11 does allow it (7.2/1 has the grammar for an enum declaration).
In fact C++ isn’t entirely backward-compatible even with C89, although this is the kind of thing that if had it been in C89, you’d expect C++ to permit it.
The key advantage to me of the trailing comma is when you write this:
and then later change it to this:
It’s nice that only one line is changed when you
diffthe versions. To get the same effect when there’s no trailing comma allowed, you could write:But (a) that’s crazy talk, and (b) it doesn’t help in the (admittedly rare) case that you want to add the new value at the beginning.