Enum is a class?

Leonid A. Broukhis leob@best.com
Fri Feb 5 14:34:00 GMT 1999


To recapitulate the subject:

enum x {
        x1, x2, x3
};

struct elem_t {
  x  a  :10;
  x  b  :10;
};

template <class T> bool operator!= (const T& x, const T & y)
{
        return !(x == y);
}

bool f(elem_t z) {

// replace a with b and watch the error disappear
// Explicit cast of z.a to x also helps!!!

         return ( x1 != z.a);
}

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

That's not me, that's stl_relop.h. I was using bitfield enums
happily until I touched the inequality operator.

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

Right; and the bug went unnoticed until STL came.

In any case, the fact that an explicit casting helps demonstrates
that something is wrong inside EGCS. The template requires the
two operands to be of the same type. The first one, x1, is of type x;
if the second one, from egcs' point of view, is of the same type
for all purposes, then the casting should not have helped; if it
is of a different type (e.g. x:10), 
then an implicit cast operation must be performed.

	Leo


More information about the Gcc-bugs mailing list