This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Enum is a class?



>Leonid A. Broukhis wrote:
>> 
>> When compiled with
>> gcc version egcs-2.91.60 19981201 (egcs-1.1.1 release)
>> 
>> The following program
>> ---- cut here ----
>> enum x {
>>         x1, x2, x3
>> };
>> 
>> struct elem_t {
>>   x  a  :10;
>>   int 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
>>          return ( x1 != z.a);
>> }
>> ---- cut here ----
>> 
>> gives
>> 
>> bug.cc: In function `bool f(struct elem_t)':
>> bug.cc:19: attempt to take address of bit-field structure member `a'
>> bug.cc:19: can't convert from incomplete type `{error}' to `const x *'
>
>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).

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

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

>which wouldn't match in your function f() because one
>argument is not "x:10" but "x". EGCS's behaviour is
>obviously inconsistent, which is bad, but should this
>*really* be allowed?

gcc 2.7.2.1, among other compilers, works fine. So it is a regression,
after all.

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

	Leo



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]