Topic: Derived* to Base* vs Derived* to bool conversion.
Author: Alexander Belopolsky <belopolsky@my-deja.com>
Date: 1999/07/15 Raw View
In article <slrn7ndkkh.20a.sbnaran@localhost.localdomain>,
sbnaran@uiuc.edu wrote:
>
> On 25 Jun 1999 22:33:48 GMT, Alexander Belopolsky
[snip]
> >g++ prefers B* to A* conversion and prints "base",
> >CC on the other hand picks f(bool) and prints "bool".
>
> IMHO, one shouldn't rely on the language here, as it might lead
> to an obscure style of programming. I would be happy with a
> compiler that gave an ambiguity error, just because.
>
The real life example comes from Qt sources (www.troll.no):
qhbox.h:
class Q_EXPORT QHBox : public QFrame
{
Q_OBJECT
public:
QHBox( QWidget *parent=0, const char *name=0, WFlags f=0, bool
allowLines=TRUE );
void setSpacing( int );
protected:
QHBox( bool horizontal, QWidget *parent=0, const char *name=0,
WFlags f=0, bool allowLines=TRUE );
void frameChanged();
private:
QBoxLayout *lay;
};
This is pretty clean design, but QHBox(&derivedWidget) will become
ambiguous with your rule.
--alex
Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: sbnaran@localhost.localdomain (Siemel Naran)
Date: 1999/07/15 Raw View
On 15 Jul 99 23:05:54 GMT, Alexander Belopolsky <belopolsky@my-deja.com> wrote:
>In article <slrn7ndkkh.20a.sbnaran@localhost.localdomain>,
>> >g++ prefers B* to A* conversion and prints "base",
>> >CC on the other hand picks f(bool) and prints "bool".
>>
>> IMHO, one shouldn't rely on the language here, as it might lead
>> to an obscure style of programming. I would be happy with a
>> compiler that gave an ambiguity error, just because.
>
>The real life example comes from Qt sources (www.troll.no):
>
>qhbox.h:
>
>class Q_EXPORT QHBox : public QFrame
>{
> Q_OBJECT
>public:
> QHBox( QWidget *parent=0, const char *name=0, WFlags f=0, bool
>allowLines=TRUE );
> void setSpacing( int );
>
>protected:
> QHBox( bool horizontal, QWidget *parent=0, const char *name=0,
>WFlags f=0, bool allowLines=TRUE );
>
> void frameChanged();
>
>private:
> QBoxLayout *lay;
>};
>
>This is pretty clean design, but QHBox(&derivedWidget) will become
>ambiguous with your rule.
I disagree. This is not clean design. It was way too many default
arguments and overloaded constructors. (But so many graphical
toolkits do, and I'm not sure what the convenience of default
arguments buys you.)
For example, consider from <qlcdnumber.h>
QLCDNumber( QWidget *parent=0, const char *name=0 );
QLCDNumber( uint numDigits, QWidget *parent=0, const char *name=0 );
I would have just had
QLCDNumber( uint numDigits, QWidget *parent, const char *name );
There's only a little more typing.
--
----------------------------------
Siemel B. Naran (sbnaran@uiuc.edu)
----------------------------------
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: sbnaran@localhost.localdomain (Siemel Naran)
Date: 1999/06/28 Raw View
On 25 Jun 1999 22:33:48 GMT, Alexander Belopolsky
>class A {};
>class B : public A{};
>void f(bool x) {
>void f(A* x) {
Does class A define an operator bool?
If so, then your overloaded functions f(bool) and f(A*) are simply
confusing. Maybe the language rules specify that f(A*) should be
called, but your human readers are likely to be confused.
>int main()
>{
> B* b = new B;
> f(b);
> delete b;
>
>}
>
>g++ prefers B* to A* conversion and prints "base",
>CC on the other hand picks f(bool) and prints "bool".
IMHO, one shouldn't rely on the language here, as it might lead
to an obscure style of programming. I would be happy with a
compiler that gave an ambiguity error, just because.
>According to Table 9 in 13.3.3.1.1 both conversions
>have "Conversion" rank. Pointer conversions are listed
>before boolean, but there is nothing in the text to say
>that the order in Table 9 is significant within the rank.
OK.
>Am I missing something? I tend to believe that g++ does
>the right thing, but I would appreciate an experts' opinion.
Sorry, don't know the exact answer.
--
----------------------------------
Siemel B. Naran (sbnaran@uiuc.edu)
----------------------------------
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Salters <msalters@lucent.com>
Date: 1999/06/29 Raw View
Siemel Naran wrote:
>
> On 25 Jun 1999 22:33:48 GMT, Alexander Belopolsky
>
> >class A {};
> >class B : public A{};
> >void f(bool x) {
> >void f(A* x) {
>
> Does class A define an operator bool?
> If so, then your overloaded functions f(bool) and f(A*) are simply
> confusing. Maybe the language rules specify that f(A*) should be
> called, but your human readers are likely to be confused.
I think you are overly worried here. Even if A defines an operator
bool in no way will a pointer to A be converted to a bool by that
user-defined cast. The only way to cast a pointer to a bool is the
built-in, which boils down to comparing it with 0 (the null pointer)
Michiel Salters
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: wmm@fastdial.net
Date: 1999/06/29 Raw View
In article <7l0qgt$ffg$1@nnrp1.deja.com>,
Alexander Belopolsky <belopolsky@my-deja.com> wrote:
> #include <iostream.h>
>
> class A {};
>
> class B : public A{};
>
> void f(bool x) {
> cout << "bool" << endl;
> }
>
> void f(A* x) {
> cout << "base" << endl;
> }
>
> int main()
> {
> B* b = new B;
> f(b);
> delete b;
>
> }
>
> According to Table 9 in 13.3.3.1.1 both conversions
> have "Conversion" rank. Pointer conversions are listed
> before boolean, but there is nothing in the text to say
> that the order in Table 9 is significant within the rank.
>
> Am I missing something?
Yes. 13.3.3.2p4 says,
Two conversion sequences with the same rank are
indistinguishable unless one of the following rules
applies:
- A conversion that is not a conversion of a pointer,
or pointer to member, to bool is better than another
conversion that is such a conversion
Although that text is a little turgid, I take it to mean that
a B* to A* conversion is better than a B* to bool conversion.
--
William M. Miller, wmm@fastdial.net
Software Emancipation Technology (www.setech.com)
Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Alexander Belopolsky <belopolsky@my-deja.com>
Date: 1999/06/25 Raw View
Dear All:
The following code produces different
results on egcs-1.1.2 and
Sun CC: WorkShop Compilers 5.0 98/12/15 C++ 5.0:
#include <iostream.h>
class A {};
class B : public A{};
void f(bool x) {
cout << "bool" << endl;
}
void f(A* x) {
cout << "base" << endl;
}
int main()
{
B* b = new B;
f(b);
delete b;
}
g++ prefers B* to A* conversion and prints "base",
CC on the other hand picks f(bool) and prints "bool".
According to Table 9 in 13.3.3.1.1 both conversions
have "Conversion" rank. Pointer conversions are listed
before boolean, but there is nothing in the text to say
that the order in Table 9 is significant within the rank.
Am I missing something? I tend to believe that g++ does
the right thing, but I would appreciate an experts' opinion.
Thank you in advance!
--alex
Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]