Enums

B

borophyll

Hi all, what is the meaning of the second form of an enum declaration

enum identifier { enumerator-list }
enum identifier { enumerator-list , }

as described in the spec. I couldn't see anywhere in the spec that
describes what this second form (with the extra comma) means?

Regards,
B.
 
R

Richard

Hi all, what is the meaning of the second form of an enum declaration

enum identifier { enumerator-list }
enum identifier { enumerator-list , }

as described in the spec. I couldn't see anywhere in the spec that
describes what this second form (with the extra comma) means?

Regards,
B.

I'm not sure how "standard" it is, but it's a convenience in some
places. It has no meaning other than to facilitate adding of new enum
constants afaik.

enum MYENUMS {
FIRST=0,
SECOND,
THIRD,
}

The benefit is debatable but I like it. The idea being you can
rearrange, or cut and paste to your hearts content without keeping your
eagle eye on missing commas.
 
P

Pietro Cerutti

Richard said:
I'm not sure how "standard" it is, but it's a convenience in some
places. It has no meaning other than to facilitate adding of new enum
constants afaik.

It's a new C99 feature, described by the Rationale as follows:

6.7.2.2 Enumeration specifiers
25
A new feature of C9X: a common extension in many implementations allows
a trailing comma
after the list of enumeration constants. The Committee decided to adopt
this feature as an
innocuous extension that mirrors the trailing commas allowed in
initializers.
 
K

Kenneth Brody

Richard wrote:
[...]
I'm not sure how "standard" it is, but it's a convenience in some
places. It has no meaning other than to facilitate adding of new enum
constants afaik.

enum MYENUMS {
FIRST=0,
SECOND,
THIRD,
}

The benefit is debatable but I like it. The idea being you can
rearrange, or cut and paste to your hearts content without keeping your
eagle eye on missing commas.

Well, consider:

enum foo {
FIRST=0,
SECOND,
#ifdef ALLOW_THIRD
THIRD,
#endif
#ifdef ALLOW_FOURTH
FOURTH,
#endif
};

versus

enum foo {
FIRST=0,
SECOND
#ifdef ALLOW_THIRD
,THIRD
#endif
#ifdef ALLOW_FOURTH
,FOURTH
#endif
};

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:[email protected]>
 
C

CBFalconer

Kenneth said:
Richard wrote:
[...]
I'm not sure how "standard" it is, but it's a convenience in some
places. It has no meaning other than to facilitate adding of new
enum constants afaik.

enum MYENUMS {
FIRST=0,
SECOND,
THIRD,
}

The benefit is debatable but I like it. The idea being you can
rearrange, or cut and paste to your hearts content without keeping
your eagle eye on missing commas.

Well, consider:

enum foo {
FIRST=0,
SECOND,
#ifdef ALLOW_THIRD
THIRD,
#endif
#ifdef ALLOW_FOURTH
FOURTH,
#endif
};

No, consider:

enum MYENUMS{FIRST = 0
,SECOMD
,THIRD
}

which is trivially easily modified in future.
 
B

Ben Bacarisse

Kenneth Brody said:
Richard wrote:
[...]
I'm not sure how "standard" it is, but it's a convenience in some
places. It has no meaning other than to facilitate adding of new enum
constants afaik.

enum MYENUMS {
FIRST=0,
SECOND,
THIRD,
}

The benefit is debatable but I like it. The idea being you can
rearrange, or cut and paste to your hearts content without keeping your
eagle eye on missing commas.

Well, consider:

enum foo {
FIRST=0,
SECOND,
#ifdef ALLOW_THIRD
THIRD,
#endif
#ifdef ALLOW_FOURTH
FOURTH,
#endif
};

versus

enum foo {
FIRST=0,
SECOND
#ifdef ALLOW_THIRD
,THIRD
#endif
#ifdef ALLOW_FOURTH
,FOURTH
#endif
};

For C90 code, there is also the option of:

enum foo {
FIRST=0,
SECOND,
#ifdef ALLOW_THIRD
THIRD,
#endif
#ifdef ALLOW_FOURTH
FOURTH,
#endif
DUMMY_FINAL_FOO_ENUM_FOR_SYNTACTIC_VALUE_ONLY
};
 
R

Richard

Ben Bacarisse said:
Kenneth Brody said:
Richard wrote:
[...]
I'm not sure how "standard" it is, but it's a convenience in some
places. It has no meaning other than to facilitate adding of new enum
constants afaik.

enum MYENUMS {
FIRST=0,
SECOND,
THIRD,
}

The benefit is debatable but I like it. The idea being you can
rearrange, or cut and paste to your hearts content without keeping your
eagle eye on missing commas.

Well, consider:

enum foo {
FIRST=0,
SECOND,
#ifdef ALLOW_THIRD
THIRD,
#endif
#ifdef ALLOW_FOURTH
FOURTH,
#endif
};

versus

enum foo {
FIRST=0,
SECOND
#ifdef ALLOW_THIRD
,THIRD
#endif
#ifdef ALLOW_FOURTH
,FOURTH
#endif
};

For C90 code, there is also the option of:

enum foo {
FIRST=0,
SECOND,
#ifdef ALLOW_THIRD
THIRD,
#endif
#ifdef ALLOW_FOURTH
FOURTH,
#endif
DUMMY_FINAL_FOO_ENUM_FOR_SYNTACTIC_VALUE_ONLY
};

I'm happy that my view is supported by both examples.
 
R

Richard

CBFalconer said:
No, consider:

enum MYENUMS{FIRST = 0
,SECOMD
,THIRD
}

which is trivially easily modified in future.

And simply "wrong" on the eye. *shrug*. IMO of course.
 
K

Keith Thompson

CBFalconer said:
Kenneth said:
Richard wrote:
[...]
I'm not sure how "standard" it is, but it's a convenience in some
places. It has no meaning other than to facilitate adding of new
enum constants afaik.

enum MYENUMS {
FIRST=0,
SECOND,
THIRD,
}

The benefit is debatable but I like it. The idea being you can
rearrange, or cut and paste to your hearts content without keeping
your eagle eye on missing commas.

Well, consider:

enum foo {
FIRST=0,
SECOND,
#ifdef ALLOW_THIRD
THIRD,
#endif
#ifdef ALLOW_FOURTH
FOURTH,
#endif
};

No, consider:

enum MYENUMS{FIRST = 0
,SECOMD
,THIRD
}

which is trivially easily modified in future.

Consider:

enum foo {
#ifdef ALLOW_FIRST
FIRST,
#endif
/* ... */
}

With the comma at the end of each line except the last, it's easily
modifiable unless you want to make a change at the end of the list.

With the comma at the beginning of each line except the first (which I
find much harder on the eyes), it's easily modifiable unless you want
to make a change at the beginning of the list.

With a comma on each line, as allowed by C99, it's easily modifiable
wherever you need to make a change.
 
A

Al Balmer

Kenneth said:
Richard wrote:
[...]
I'm not sure how "standard" it is, but it's a convenience in some
places. It has no meaning other than to facilitate adding of new
enum constants afaik.

enum MYENUMS {
FIRST=0,
SECOND,
THIRD,
}

The benefit is debatable but I like it. The idea being you can
rearrange, or cut and paste to your hearts content without keeping
your eagle eye on missing commas.

Well, consider:

enum foo {
FIRST=0,
SECOND,
#ifdef ALLOW_THIRD
THIRD,
#endif
#ifdef ALLOW_FOURTH
FOURTH,
#endif
};

No, consider:

enum MYENUMS{FIRST = 0
,SECOMD
,THIRD
}

which is trivially easily modified in future.
You've just moved the problem from the bottom to the top.
 
C

Chris Thomasson

Richard said:
Hi all, what is the meaning of the second form of an enum declaration

enum identifier { enumerator-list }
enum identifier { enumerator-list , }
[...]
The benefit is debatable but I like it. The idea being you can
rearrange, or cut and paste to your hearts content without keeping your
eagle eye on missing commas.

The compiler will keep an eye on the syntax errors for you...

;^)
 
C

Chris Thomasson

[...]
With a comma on each line, as allowed by C99, it's easily modifiable
wherever you need to make a change.

How about this macro hack:

____________
/* support */
#define ENUM_PUSH_FRONT(mp_name)mp_name
#define ENUM_PUSH_BACK(mp_name),mp_name
#define ENUM_PUSH_FRONT_EX(mp_name, mp_val) \
ENUM_PUSH_FRONT(mp_name) = (mp_val)
#define ENUM_PUSH_BACK_EX(mp_name, mp_val) \
ENUM_PUSH_BACK(mp_name) = (mp_val)

/* origin */
enum foo_e {
ENUM_PUSH_FRONT_EX(FOO_FLAG_ONE, 0x1)
ENUM_PUSH_BACK_EX(FOO_FLAG_TWO, 0x2)
ENUM_PUSH_BACK_EX(FOO_FLAG_THREE, 0x4)
ENUM_PUSH_BACK_EX(FOO_FLAG_FOUR, 0x8)
};


/* edited *//*
enum foo_e {
ENUM_PUSH_FRONT_EX(FOO_FLAG_ONE, 0x1)
ENUM_PUSH_BACK_EX(FOO_FLAG_TWO, 0x2)
ENUM_PUSH_BACK_EX(FOO_FLAG_THREE, 0x4)
ENUM_PUSH_BACK_EX(FOO_FLAG_FOUR, 0x8)
ENUM_PUSH_BACK_EX(FOO_FLAG_FIVE, 0x10)
ENUM_PUSH_BACK_EX(FOO_FLAG_SIX, 0x20)
};
*/


/* edited some more *//*
enum foo_e {
ENUM_PUSH_FRONT_EX(FOO_FLAG_ONE, 0x1)
ENUM_PUSH_BACK_EX(FOO_FLAG_TWO, 0x2)
ENUM_PUSH_BACK_EX(FOO_FLAG_THREE, 0x4)
ENUM_PUSH_BACK(FOO_SOMETHING)
ENUM_PUSH_BACK(FOO_SOMETHING_ELSE)
};
*/

____________


?
 

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
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top