Topic: cannot compare base* to const derrived*
Author: einwegadresse@gmx.de (Immanuel Scholz)
Date: Thu, 24 Jul 2003 02:10:31 +0000 (UTC) Raw View
Hi group.
Today, I tried the following statement and got an compiler error:
class Base {};
class Derrived : public Base {};
int main()
{
Base* b = 0;
const Derrived* d = 0;
return (b == d);
}
The compiler refuses to convert either Base* to const Derrived*
(downcast) or convert const Derrived* to Base* (constcast).
But it could convert both, Base* to const Base* and const Derrived* to
const Base*.
Why refuses the compiler to resolve the casts? Is it an error within
the compiler?
I thought it is only forbidden to implicit convert one function
argument twice, but not to convert more than one argument only once?
Am I correct?
If the compiler is right, is there a reasoning about why it is
forbidden?
Ciao, Imi.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: richard@ex-parrot.com (Richard Smith)
Date: Thu, 24 Jul 2003 18:05:34 +0000 (UTC) Raw View
Immanuel Scholz wrote:
> Today, I tried the following statement and got an compiler error:
>
> class Base {};
> class Derrived : public Base {};
>
> int main()
> {
> Base* b = 0;
> const Derrived* d = 0;
> return (b == d);
> }
I don't know which compiler you're using, but it should
work, and does on all the compilers that I tried.
> The compiler refuses to convert either Base* to const Derrived*
> (downcast) or convert const Derrived* to Base* (constcast).
Correct.
> But it could convert both, Base* to const Base* and const Derrived* to
> const Base*.
This is precisely what it should do.
> Why refuses the compiler to resolve the casts? Is it an error within
> the compiler?
Yes, I think so.
> I thought it is only forbidden to implicit convert one function
> argument twice, but not to convert more than one argument only once?
> Am I correct?
Broadly speaking, yes, although it's actually not quite that
simple. If you want the gory details, look in section
13.3.3.1 in the Standard.
--
Richard Smith
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: kuyper@wizard.net (James Kuyper)
Date: Thu, 24 Jul 2003 20:55:38 +0000 (UTC) Raw View
einwegadresse@gmx.de (Immanuel Scholz) wrote in message news:<7df0cdd.0307230321.1852873d@posting.google.com>...
> Hi group.
>
> Today, I tried the following statement and got an compiler error:
>
>
> class Base {};
> class Derrived : public Base {};
>
> int main()
> {
> Base* b = 0;
> const Derrived* d = 0;
> return (b == d);
> }
>
>
> The compiler refuses to convert either Base* to const Derrived*
> (downcast) or convert const Derrived* to Base* (constcast).
>
> But it could convert both, Base* to const Base* and const Derrived* to
> const Base*.
>
> Why refuses the compiler to resolve the casts? Is it an error within
> the compiler?
>
> I thought it is only forbidden to implicit convert one function
> argument twice, but not to convert more than one argument only once?
> Am I correct?
>
> If the compiler is right, is there a reasoning about why it is
> forbidden?
Your code looks fine to me, and is accepted by my compiler: gcc
version 2.96 20000731 (Red Hat Linux 7.1 2.96-85).
What precisely do you get as an error message for your code? Which
compiler are you using?
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: "Victor Bazarov" <v.Abazarov@attAbi.com>
Date: Thu, 24 Jul 2003 20:27:56 CST Raw View
"Immanuel Scholz" <einwegadresse@gmx.de> wrote...
> Today, I tried the following statement and got an compiler error:
>
>
> class Base {};
> class Derrived : public Base {};
>
> int main()
> {
> Base* b = 0;
> const Derrived* d = 0;
> return (b == d);
> }
>
>
> The compiler refuses to convert either Base* to const Derrived*
> (downcast) or convert const Derrived* to Base* (constcast).
Neither cast can be made implicitly, both require explicit casts.
However, 'b' can be converted to const Base* and 'd' can be
converted to const Base*, and both are accepted. That's what
the compiler should do.
What did the compiler say? I tried this on Comeau and it gladly
accepted the code.
> But it could convert both, Base* to const Base* and const Derrived* to
> const Base*.
>
> Why refuses the compiler to resolve the casts? Is it an error within
> the compiler?
Very well could be.
> I thought it is only forbidden to implicit convert one function
> argument twice, but not to convert more than one argument only once?
> Am I correct?
Probably. Rules of overload resolution are so many and so...
How should I put it?... murky. At least to me. I remember that
for the assignment operator the left operand is not converted.
As to other conversions, I am blanking out for a moment.
> If the compiler is right, is there a reasoning about why it is
> forbidden?
I think the compiler is wrong.
Victor
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: gp1@paradise.net.nz (Graeme Prentice)
Date: Fri, 25 Jul 2003 08:31:29 +0000 (UTC) Raw View
On Thu, 24 Jul 2003 02:10:31 +0000 (UTC), einwegadresse@gmx.de (Immanuel
Scholz) wrote:
>Hi group.
>
>Today, I tried the following statement and got an compiler error:
>
>
>class Base {};
>class Derrived : public Base {};
>
>int main()
>{
> Base* b = 0;
> const Derrived* d = 0;
> return (b == d);
>}
>
>
>The compiler refuses to convert either Base* to const Derrived*
>(downcast) or convert const Derrived* to Base* (constcast).
>
The relevant section in the standard is 5.9 para 2 "relational
operators" where it says
Pointer conversions (4.10) and qualification conversions (4.4) are
performed on pointer operands (or on a pointer operand and a null
pointer constant) to bring them to their composite pointer type.
4.10 says conversion of Derived* to Base* is a standard (implicit)
conversion (providing no ambiguity or accessibility problem) and 4.4
allows conversion of Base* to const Base* (increase CV qualification)
and both standard pointer conversions and qualification conversions can
be applied to either operand e.g. if you have
const Base *b;
Derived * d;
it could implicitly convert d to const Base *
>But it could convert both, Base* to const Base* and const Derrived* to
>const Base*.
>
>Why refuses the compiler to resolve the casts? Is it an error within
>the compiler?
There are no casts in your code. A cast is an explicit type conversion
e.g.
return ( static_cast<const Base *>(b) ==
static_cast<const Base *>(d) );
>
>I thought it is only forbidden to implicit convert one function
>argument twice, but not to convert more than one argument only once?
>Am I correct?
It sounds like you are referring to conversions applying to arguments in
a function call - 13.3.3.1.2 says only one user defined conversion can
be applied in an implicit conversion sequence - but a user defined
conversion sequence can involve two separate standard conversions.
Graeme
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]