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

And in any case such a definition is dangerous; you'd be
better specifically defining your != operator for your
type.

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

which means that the parameters are references to an "x".
But the bitfield is a bitfield, not a proper "x", and only
gets converted to an "x" when you read it; even if you're
claiming that the type is not "x", but "x:10" or something,
the operator instantiated would then be:

   bool operator != (x:10 &a, x:10 &b)
   {
      return !(a == b);
   }

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?

I'll be interested to hear what the C++ standard says.

Thanks,

Alastair.

____________________________________________________________
Alastair Houghton                              ajh8@ic.ac.uk
(ISE 3)                             Imperial College, London


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