Topic: Possible defect: Derived * to Base * const & conversion


Author: rani_sharoni@hotmail.com (Rani Sharoni)
Date: Thu, 20 Jun 2002 17:31:02 GMT
Raw View
Hello,

I know that implicit conversion from D ** to B * const *, when D
derived from B, is not allowed because it can be problematic when
dealing with multiple inheritance.

I assumed that if this implicit conversion is not allowed then I would
not be able to perform this conversion using several (standard)
implicit conversions. But I think that the following code show that it
is possible:

struct B {};
struct D : B {};

D  *dp = new D;

B * const &bpr = dp;   // this conversion is allowed
B * const *bpp = &bpr; // opps

// The direct conversion is forbidden
// Comeau: a value of type "D **" cannot be used to
// initialize an entity of type "B *const *"
B * const *bpp2 = &dp;

I compiled the above code using Comeau C/C++ 4.3.0 online.

My questions:
1) Why the conversion from D* to B * const & is allowed?
2) Is the code above reveals defect in the language or are there other
examples for such behavior where several (standard) implicit
conversions can bypass some illegal implicit conversion?

Thanks,
Rani

---
[ 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: rani_sharoni@hotmail.com (Rani Sharoni)
Date: Fri, 21 Jun 2002 05:06:09 GMT
Raw View
rani_sharoni@hotmail.com (Rani Sharoni) wrote in message > My questions:

Hello,

I'm sorry for being hasty in my previous post.
Later I understood that the language is perfectly consistent regarding
the mentioned conversion.
Although such conversions are perfectly safe it can be surprising to
the programmer and cause more harm then profit and this is probably
the real reason for forbidding them.

Here is an example that clarify my mistake:

void f1(double const *);
void f2(double *);

void g()
{
    int a = 10;
    // this one is analog to Derived * to Base * const & conversion
    const double &dref = a;
    f1(&dref); // fine. dref points to the temporary, but f1(&a) is
forbidden

    double d = a;
    f2(&d); // fine. points to the d, but f2(&a) is forbidden.
}

In both cases, although the conversion is possible via several
standard conversions, the direct conversion is not allowed, again
probably because it might surprise the programmer.

Answers to my naive questions:
> 1) Why the conversion from D* to B * const & is allowed?
Convert D * to B * and then bind to const reference.
> 2) Is the code above reveals defect in the language or are there other
> examples for such behavior where several (standard) implicit
> conversions can bypass some illegal implicit conversion?
As shown above there are common cases where T1 is converted to T2 and
as a result there are some several standard conversions in which T1 *
is converted to T2 cv-opt * (like T1 to T2 and then T2 * to T2 *), but
the direct conversion is forbidden.

Sorry for making such mistake in this newsgroup,
Rani

---
[ 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                       ]