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 10:25:25 +0000
- Organization: Imperial College of Science, Medicine and Technology, London
- References: <199902050023.QAA09847@shell7.ba.best.com>
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