Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Enums

161 views
Skip to first unread message

boro...@gmail.com

unread,
Sep 25, 2007, 7:38:59 AM9/25/07
to
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.

Richard

unread,
Sep 25, 2007, 8:07:33 AM9/25/07
to
boro...@gmail.com writes:

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.

Pietro Cerutti

unread,
Sep 25, 2007, 8:26:40 AM9/25/07
to
Richard wrote:
> boro...@gmail.com writes:
>
>> 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.

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.

>
> 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.
>


--
Pietro Cerutti

PGP Public Key:
http://gahr.ch/pgp

Kenneth Brody

unread,
Sep 25, 2007, 1:29:51 PM9/25/07
to
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:ThisIsA...@gmail.com>


Ben Bacarisse

unread,
Sep 25, 2007, 6:23:36 PM9/25/07
to
Kenneth Brody <kenb...@spamcop.net> writes:

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
};

--
Ben.

Richard

unread,
Sep 25, 2007, 6:44:13 PM9/25/07
to
Ben Bacarisse <ben.u...@bsb.me.uk> writes:

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

CBFalconer

unread,
Sep 25, 2007, 6:02:54 PM9/25/07
to
Kenneth Brody wrote:
> 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.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Richard

unread,
Sep 25, 2007, 7:44:48 PM9/25/07
to
CBFalconer <cbfal...@yahoo.com> writes:

>
> 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.

Keith Thompson

unread,
Sep 25, 2007, 8:05:45 PM9/25/07
to

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.

--
Keith Thompson (The_Other_Keith) ks...@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Al Balmer

unread,
Sep 26, 2007, 12:09:49 PM9/26/07
to
On Tue, 25 Sep 2007 18:02:54 -0400, CBFalconer <cbfal...@yahoo.com>
wrote:

You've just moved the problem from the bottom to the top.

--
Al Balmer
Sun City, AZ

Chris Thomasson

unread,
Sep 26, 2007, 5:44:33 PM9/26/07
to
"Richard" <rgr...@gmail.com> wrote in message
news:59ans4-...@news.individual.net...

> boro...@gmail.com writes:
>
>> 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...

;^)

Chris Thomasson

unread,
Sep 26, 2007, 7:12:05 PM9/26/07
to
"Keith Thompson" <ks...@mib.org> wrote in message
news:lnhclix...@nuthaus.mib.org...
[...]

> 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)
};
*/

____________


?

0 new messages