Question

Take this enum as an example to the question

enum
{
     one = 1
   , two = 2
   , three = 3
};

I've compiled this enumeration with the following compilers without issue:

  • Visual Studio 2010
  • IAR Embedded Workbench for ARM 6.21
  • g++ (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3
  • gcc (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3

Is this syntax guaranteed to be supported under C99 and C++03?

With that said, I'd like to avoid a discussion about why use this syntax.

Was it helpful?

Solution

Extra whitespace or the specific choices of spaces is entirely irrelevant in the context of defining an enum and the enumerators are separate by comma. Thus, the quote way to define enumerators is certainly covered by the C++ standard (all versions thereof) and by C99 and later standards (I don't make any claims for C90 because I'm not sufficiently familiar with C90 and I don't have it handy but I think the enum definition works there, too). Also, in all these cases the enum can be anonymous.

The interesting question is support for a trailing comma after the last enumerators, e.g.

enum { value, };

which isn't covered by all standards. It is covered in C99 and later. I don't think a trailing comma is supported by C++03 but it is supported by C++11.

OTHER TIPS

The number and choice of character for whitespace between tokens doesn't matter in the C and C++ languages. So, that's exactly the same as enum {one=1,two=2,three=3};. The only time you need SOME whitespace is if you have two keywords with no "non-word" separators such as comma, parenthesis, colon, etc.

So yes, if that's not acceptable in a compiler, that compiler is a non-standard-conforming compiler.

C and C++ allow you to add an arbitrary number of whitespaces and newlines between tokens.

So this is equivalent to:

enum
{
   one = 1,
   two = 2,
   three = 3
};

But of course yours has a somewhat unusual style...

Yes , guaranteed to be supported under C99 and C++03

 enum
{
     one = 1
   , two = 2
   , three = 3
};  

is considered as same as below

enum
{
     one = 1 ,
     two = 2 ,
     three = 3 
};  

or

enum { one = 1 , two = 2 , three = 3 };

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top