Enums

Discussion in 'C Programming' started by borophyll@gmail.com, Sep 25, 2007.

  1. Guest

    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.
     
    , Sep 25, 2007
    #1
    1. Advertising

  2. Richard Guest

    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.

    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.
     
    Richard, Sep 25, 2007
    #2
    1. Advertising

  3. Richard wrote:
    > 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
     
    Pietro Cerutti, Sep 25, 2007
    #3
  4. 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:>
     
    Kenneth Brody, Sep 25, 2007
    #4
  5. CBFalconer Guest

    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
     
    CBFalconer, Sep 25, 2007
    #5
  6. Kenneth Brody <> writes:

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

    --
    Ben.
     
    Ben Bacarisse, Sep 25, 2007
    #6
  7. Richard Guest

    Ben Bacarisse <> writes:

    > Kenneth Brody <> writes:
    >
    >> 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.
     
    Richard, Sep 25, 2007
    #7
  8. Richard Guest

    CBFalconer <> 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.
     
    Richard, Sep 26, 2007
    #8
  9. CBFalconer <> writes:
    > 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.


    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) <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"
     
    Keith Thompson, Sep 26, 2007
    #9
  10. Al Balmer Guest

    On Tue, 25 Sep 2007 18:02:54 -0400, CBFalconer <>
    wrote:

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

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

    --
    Al Balmer
    Sun City, AZ
     
    Al Balmer, Sep 26, 2007
    #10
  11. "Richard" <> wrote in message
    news:...
    > 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, Sep 26, 2007
    #11
  12. "Keith Thompson" <> wrote in message
    news:...
    [...]
    > 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)
    };
    */

    ____________


    ?
     
    Chris Thomasson, Sep 27, 2007
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Syd

    Loop through enums

    Syd, Aug 28, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    3,046
    John Saunders
    Aug 28, 2003
  2. martin

    enums

    martin, May 28, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    616
    Brian W
    May 28, 2004
  3. Hans De Schrijver

    Basic question about enums

    Hans De Schrijver, Jun 5, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    350
    Peter Rilling
    Jun 5, 2004
  4. Matt

    Storing Enums in web.config

    Matt, Jun 7, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    2,621
  5. =?utf-8?b?QXNiasO4cm4gU8OmYsO4?=

    Enums without identifier, enums and typedef

    =?utf-8?b?QXNiasO4cm4gU8OmYsO4?=, Jan 19, 2007, in forum: C Programming
    Replies:
    10
    Views:
    1,143
    Keith Thompson
    Jan 20, 2007
Loading...

Share This Page