Topic: enum


Author: comeau@panix.com (Greg Comeau)
Date: 1999/06/13
Raw View
In article <37623e5d.3603257@news.freeserve.net> John_Maddock@compuserve.com (John Maddock) writes:
>I wonder if anyone can tell me if there is a difference between the C
>and C++ enum syntax, specifically:
>
>enum tag{
> value=1,
>};
>
>seems to be allowed by C but not C++ because of the trailing comma,
>any comments?

I don't recall offhand that either language allows for it, though it's
probably a common extension.

- Greg
--
       Comeau Computing, 91-34 120th Street, Richmond Hill, NY, 11418-3214
     Producers of Comeau C/C++ 4.2.38 -- New Release!  We now do Windows too.
    Email: comeau@comeaucomputing.com / Voice:718-945-0009 / Fax:718-441-2310
                *** WEB: http://www.comeaucomputing.com ***


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: juergen@monocerus.demon.co.uk (Juergen Heinzl)
Date: 1999/06/13
Raw View
In article <37623e5d.3603257@news.freeserve.net>, John Maddock wrote:
>I wonder if anyone can tell me if there is a difference between the C
>and C++ enum syntax, specifically:
>
>enum tag{
> value=1,
>};
>
>seems to be allowed by C but not C++ because of the trailing comma,
>any comments?

Yes, the trailing ',' is not allowed by ISO C++.

Cheers,
Juergen

--
\ Real name     : J   rgen Heinzl                 \       no flames      /
 \ EMail Private : juergen@monocerus.demon.co.uk \ send money instead /


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: jpotter@falcon.lhup.edu (John Potter)
Date: 1999/06/13
Raw View
John_Maddock@compuserve.com (John Maddock) wrote:

: I wonder if anyone can tell me if there is a difference between the C
: and C++ enum syntax, specifically:

: enum tag{
:  value=1,
: };

: seems to be allowed by C but not C++ because of the trailing comma,
: any comments?

You may have been thinking of

int a[] = { 1, 2, 3, };

Where the trailing , is allowed.  See 8.5.

It would be nice if there were some consistency.

John



[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: John_Maddock@compuserve.com (John Maddock)
Date: 1999/06/13
Raw View
>Sort of, but not yet. It's not allowed under the C++ grammar, nor by
>C89, but it is marked in the C9X draft standard as one of the major
>changes from the previous edition.

Thanks for the clarification, and to everyone who responded to this.


John Maddock
http://ourworld.compuserve.com/homepages/John_Maddock/
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: comeau@panix.com (Greg Comeau)
Date: 1999/06/13
Raw View
In article <E1F83.42027$wk2.602085@newscene.newscene.com> jpotter@falcon.lhup.edu (John Potter) writes:
>
>John_Maddock@compuserve.com (John Maddock) wrote:
>
>: I wonder if anyone can tell me if there is a difference between the C
>: and C++ enum syntax, specifically:
>
>: enum tag{
>:  value=1,
>: };
>
>: seems to be allowed by C but not C++ because of the trailing comma,
>: any comments?
>
>You may have been thinking of
>
>int a[] = { 1, 2, 3, };
>
>Where the trailing , is allowed.  See 8.5.
>
>It would be nice if there were some consistency.

Right, both C and C++ allow it in an initializer-list.
As has been pointed out C9X is allowing this for the enumerator-lists.

As I recall the original situation, it was sloppy C compilers VERY
early on.  Then it was justified because then machine generated code
(programs that write other C programs) didn't need to be concerned about
checking when they hit the last initializer.

Whether this is consistent or not is a mized bag.
Yes, enum's lists would now also allow the trailing comma so it
would be syntactically consistent that way.  But this becomes very
suspect in hand-written code.  Was it a mistake?  a typo? an editor goof?
Or is it a statement that's it's ok but the programmer expect for
additional initializers or enumerators.  Not too consistent that way.
Doesn't seem to support self-documenting code either.

- Greg
--
       Comeau Computing, 91-34 120th Street, Richmond Hill, NY, 11418-3214
     Producers of Comeau C/C++ 4.2.38 -- New Release!  We now do Windows too.
    Email: comeau@comeaucomputing.com / Voice:718-945-0009 / Fax:718-441-2310
                *** WEB: http://www.comeaucomputing.com ***
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: John_Maddock@compuserve.com (John Maddock)
Date: 1999/06/12
Raw View
I wonder if anyone can tell me if there is a difference between the C
and C++ enum syntax, specifically:

enum tag{
 value=1,
};

seems to be allowed by C but not C++ because of the trailing comma,
any comments?

Thanks,

John Maddock
http://ourworld.compuserve.com/homepages/John_Maddock/
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: James Kuyper <kuyper@wizard.net>
Date: 1999/06/12
Raw View
John Maddock wrote:
>
> I wonder if anyone can tell me if there is a difference between the C
> and C++ enum syntax, specifically:
>
> enum tag{
>  value=1,
> };
>
> seems to be allowed by C but not C++ because of the trailing comma,
> any comments?

Sort of, but not yet. It's not allowed under the C++ grammar, nor by
C89, but it is marked in the C9X draft standard as one of the major
changes from the previous edition.
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: jackklein@att.net (Jack Klein)
Date: 1999/06/12
Raw View
On 12 Jun 99 14:07:19 GMT, John_Maddock@compuserve.com (John Maddock)
wrote in comp.std.c++:

> I wonder if anyone can tell me if there is a difference between the C
> and C++ enum syntax, specifically:
>
> enum tag{
>  value=1,
> };
>
> seems to be allowed by C but not C++ because of the trailing comma,
> any comments?
>
> Thanks,
>
> John Maddock
> http://ourworld.compuserve.com/homepages/John_Maddock/

<Jack>

Actually it is illegal under the current C standard as well.  It may
just be that you are using a compiler which accepts it, many do.  It
will explicitly become legal in C when the C9X (C2000?) update is
adopted.

</Jack>
--
Do not email me with questions about programming.
Post them to the appropriate newsgroup.
Followups to my posts are welcome.
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Michael Viking <nospam@techne-sys.com>
Date: 1997/12/02
Raw View
Greetings,
In C, names in different enumerations must be distinct.  Why was this
particular defect kept in C++?
I looked at the section in the D&E about enums and there wasn't much
there.  I'm curious as to why they were left the same as in
C--especially when Bjarne Stroustrup says "C enumerations constitute a
curiously half-baked concept." Forcing one to have different names
seems to be the most half-baked concept of all.  It also seems that it
would be easy to allow the same name to occur in different enumerations
and then rely on the scope operator in ambiguous situations:

enum foo
{
    var1;
};

enum bar
{
    var1;
};

var1; // error--ambiguous
foo::var1; // fine
bar::var1; // fine

Couldn't this change be made without breaking any C code?
I realize that I can wrap the enums in a class or namespace,
but that solution somehow seems redundant.
Moreover, it also says in the D&E that "I had no need for enumerations
in the styles of programming I wished to support..."  That makes me
feel as if I shouldn't be using enums in my programs.  Yet I frequently come
across situations where an enum seems to fit quite nicely: there is a
short list of possible values and I want some type checking.  Sure,
enums don't have to be initialized and sure one can cast garbage to
an enum, but one can circumvent almost anything (although some things
are harder to do than others, and I realize enums can easily be broken
on accident)
In Meyer's Effective C++ item 46, it demonstrates some defects of
enum and shows a very pleasing and elegant solution.  Should I
scrap all enums from now on and use his approach?  Are there other
approaches?  Meyer's solution feels like overkill in situations such as
the example (shortened) given in Lakos' Large-Scale C++ Software
Design:

class Whatever
{
    public:
        enum Status { A, B, C };
        Status DoIt();
};

This seems like a useful construct...what's the elegant solution here?
If I 'grepped' for enum at AT&T, would it really return nothing??
Cheers,
-Michael Nyman

--
replace nospam with Michael.Nyman to reply
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: "Paul D. DeRocco" <pderocco@ix.netcom.com>
Date: 1997/12/06
Raw View
Michael Viking wrote:
>
> In C, names in different enumerations must be distinct.  Why was this
> particular defect kept in C++?

I think it has to do with reverse compatibility. Consider the case of legacy
code that has an enumerator of a particular name, and code that refers to it
without qualification by its enumeration name. Then, assume they changed the
rules in C++, and someone else came along as said, Ahah! now I can use that
name over again, as long as I qualify it properly. As soon as the legacy code
is recompiled, an ambiguity would arise, necessitating going back in and
editing the legacy code.

It might have been worth it, though.

--

Ciao,
Paul
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]