Is this a standard?

L

lallous

Hello,

enum eErrors
{
eSuccess = 0,
eErr1 = 1,
eErr2 = 2,
eErr3 = 3,
};

Notice the last comma. Is this allowed by the standard or the compiler?
Or even using: int ar[] = {1,2,3,};

p.s: VC++ 6
 
J

Jacques Labuschagne

lallous said:
Hello,

enum eErrors
{
eSuccess = 0,
eErr1 = 1,
eErr2 = 2,
eErr3 = 3,
};

Notice the last comma. Is this allowed by the standard or the compiler?
Or even using: int ar[] = {1,2,3,};

p.s: VC++ 6

Not in C++, but it used to be a common thing in C. The C++ grammar is

enum-specifier:
enum identifier(opt) { enumerator-list(opt) }
enumerator-list:
enumerator-definition
enumerator-list, enumerator-definition
enumerator-definition
enumerator
enumerator = constant-expression
enumerator:
identifier
 
T

tom_usenet

Hello,

enum eErrors
{
eSuccess = 0,
eErr1 = 1,
eErr2 = 2,
eErr3 = 3,
};

Notice the last comma. Is this allowed by the standard or the compiler?

That isn't standard (7.2/1 doesn't allow a terminating comma). Your
compiler may allow it as an extension (give it a go), but you should
avoid it.
Or even using: int ar[] = {1,2,3,};

That is standard (8.5/1 does optionally allow a terminating , for
aggregate initializers).

Tom

C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
 
A

Andrey Tarasevich

lallous said:
Hello,

enum eErrors
{
eSuccess = 0,
eErr1 = 1,
eErr2 = 2,
eErr3 = 3,
};

Notice the last comma. Is this allowed by the standard or the compiler?
Or even using: int ar[] = {1,2,3,};

In C++ and C89/C90 the former is ill-formed, while the latter is OK. In
C99 both are OK.
 
J

Jack Klein

Hello,

enum eErrors
{
eSuccess = 0,
eErr1 = 1,
eErr2 = 2,
eErr3 = 3,
};

Notice the last comma. Is this allowed by the standard or the compiler?
Or even using: int ar[] = {1,2,3,};

p.s: VC++ 6

The fact that the trailing comma is allowed in array initializations
but not in enum definitions was merely due to an accidental formatting
oversight in the 1989 ANSI C standard, adopted by ISO in 1990.

In the latest 1999 version of the C standard, the oversight was
corrected so if you have a C99 conforming compiler (and not many are),
the trailing comma is allowed in enums, but in C90 and C++, which
standard is based on the earlier C standard, it is illegal.

Many, perhaps most, C compilers and at least some C++ compilers accept
the trailing comma as a harmless extension.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,159
Messages
2,570,879
Members
47,414
Latest member
GayleWedel

Latest Threads

Top