This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
Re: Enum is a class?
- To: "Leonid A. Broukhis" <leob at best dot com>, EGCS-bugs <egcs-bugs at cygnus dot com>
- Subject: Re: Enum is a class?
- From: "Alastair J. Houghton" <ajh8 at doc dot ic dot ac dot uk>
- Date: Fri, 05 Feb 1999 21:50:33 +0000
- Organization: Imperial College of Science, Medicine and Technology, London
- References: <199902051926.LAA12672@shell7.ba.best.com>
Leonid A. Broukhis wrote:
>
> >Leonid A. Broukhis wrote:
> [snip]
>
> >Are you even allowed references to bitfield members? I
>
> No, references to bitfields are not allowed, and they never were.
>
> >don't think that your program is valid C++ - even if it
> >works on Sun's compiler, I think you should have written
> >
> > template <class T> bool operator!= (T a, T b)
> > {
> > return !(a == b);
> > }
>
> STL does it with reference arguments, and it makes sense, otherwise
> it will be extremely inefficient or plain wrong (e.g. if a copy constructor
> inserts unique numbers somewhere).
Granted that it is inefficient if the function isn't expanded
inline, or the copy constructor is too complex for the
compiler to inline and simplify.
> >And in any case such a definition is dangerous; you'd be
> >better specifically defining your != operator for your
> >type.
>
> Tell it to the STL authors. :-)
I would if I saw them ;-) ;-). I didn't think that they'd
defined such an operator... maybe I'd better have a good look
at the STL.
> >The trouble is that the type of "z.a" is "x", not "x:10";
> >so the function that gets instantiated will be
> >
> > bool operator != (x &a, x &b)
> > {
> > return !(a == b);
> > }
>
> The trouble is that the operator!= must not have been instantiated
> in the first place. It doesn't get instantiated for int comparison,
> why are enums any different?
Ah - I see what you mean. What you're saying is that the
compiler shouldn't instantiate your != operator in the first
place, because the type can't be matched. Hmmm.
> >I'll be interested to hear what the C++ standard says.
>
> Me too, but if it says that enums are classes, it is a
> major bummer.
I wouldn't be too surprised. Besides I think that the
compiler should attempt to instantiate the operator, as
you've defined it for all types - that is what
template<class T> is supposed to mean, isn't it? And it
would be bad to have behaviour defined for an enum half
of the time. You're right though - it is a bit strange
that it does one thing for "int" and another for "enum x"
types.
Maybe you should simply define a more specific operator
for the "x" type; after all, it's only an integer, so
you don't really need to use a reference...
OK. I just checked in the EGCS source. Your problem is
caused by a piece of code written by Jason Merrill
(jason@cygnus.com). It appears that the C++ standard
does specify that enums are subject to operator
overloading, though they aren't full class types.
The change, for your interest, was made in '94, so
any EGCS version later than that will not compile
your code.
Bitfields are, of course, allowed to use any integral
type.
Hope this helps,
Alastair.
____________________________________________________________
Alastair Houghton ajh8@ic.ac.uk
(ISE 3) Imperial College, London