Trailing commas and C++

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:

enum Channel {
    RED,
    GREEN,
    BLUE,
};

and then later change it to this:

enum Channel {
    RED,
    GREEN,
    BLUE,
    ALPHA,
};

It’s nice that only one line is changed when you diff the versions. To get the same effect when there’s no trailing comma allowed, you could write:

enum Channel {
    RED
   ,GREEN
   ,BLUE
};

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.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)