* temporary auto_ptr
@ 2005-09-29 11:53 Dima Sorkin
2005-09-29 12:11 ` John Love-Jensen
0 siblings, 1 reply; 6+ messages in thread
From: Dima Sorkin @ 2005-09-29 11:53 UTC (permalink / raw)
To: gcc-help
Hi.
Some tricky question, just to understand how compiler thinks:
See the example:
//a.cpp -*-c++-*- -------------------------------------------------
#include<memory>
using namespace std;
class A{};
class B : public A {};
// This function does pass comilation
auto_ptr<const A> f1(){
auto_ptr<const B> pB(new B);
return auto_ptr<const A>(pB);
}
// This function doesn't pass compilation : why ?
auto_ptr<const A> f2(){
auto_ptr<const B> pB(new B);
return pB;
} //---------------------------------------------------------------------------
Note: there exists
template<class X> template<class Y>
auto_ptr<X>::auto_ptr(auto_ptr<Y> &); non-explicit constructor
Thank you.
Dima.
P.S.
I tried to parse compiler's warnings and to understand what it does.
gcc 4.0.1 just writes that conversion is ambiguous.
gcc 3.3.4 : (as I understood)
I don't undersand why in case of "f2" compiler tries to create a const
temporary of auto_ptr<const A> as a part of cast process,
for building a non-const temporary that "f2" returns
(assuming there is no return-value optimizations in this stage).
It can construct a non-const auto_ptr<const A> temporary that "f2"
returns directly from auto_ptr<const B> instead.
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: temporary auto_ptr
2005-09-29 11:53 temporary auto_ptr Dima Sorkin
@ 2005-09-29 12:11 ` John Love-Jensen
2005-09-29 12:23 ` Dima Sorkin
0 siblings, 1 reply; 6+ messages in thread
From: John Love-Jensen @ 2005-09-29 12:11 UTC (permalink / raw)
To: Dima Sorkin, MSX to GCC
Hi Dima,
>This function doesn't pass compilation : why ?
The f2 function returns an auto_ptr<const A>.
You are trying to return an auto_ptr<const B>.
An auto_ptr<const B> is not an auto_ptr<const A>, regardless if a "B is an
A".
HTH,
--Eljay
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: temporary auto_ptr
2005-09-29 12:11 ` John Love-Jensen
@ 2005-09-29 12:23 ` Dima Sorkin
2005-09-29 12:26 ` John Love-Jensen
0 siblings, 1 reply; 6+ messages in thread
From: Dima Sorkin @ 2005-09-29 12:23 UTC (permalink / raw)
To: John Love-Jensen; +Cc: gcc-help
On 9/29/05, John Love-Jensen wrote:
> The f2 function returns an auto_ptr<const A>.
> You are trying to return an auto_ptr<const B>.
> An auto_ptr<const B> is not an auto_ptr<const A>, regardless if a "B is an
> A".
And it does not matter that there is an implicit conversion possible
between auto_ptr<const B>& and auto_ptr<const A> ?
(unless I explicitly state I want to do it, as in "f1" )
Regards,
Dima.
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: temporary auto_ptr
2005-09-29 12:23 ` Dima Sorkin
@ 2005-09-29 12:26 ` John Love-Jensen
2005-09-29 12:35 ` Dima Sorkin
0 siblings, 1 reply; 6+ messages in thread
From: John Love-Jensen @ 2005-09-29 12:26 UTC (permalink / raw)
To: Dima Sorkin; +Cc: MSX to GCC
Hi Dima,
> And it does not matter that there is an implicit conversion possible
> between auto_ptr<const B>& and auto_ptr<const A> ?
> (unless I explicitly state I want to do it, as in "f1" )
If there is a *single* implicit conversion possible, then I think you'll get
that implicit conversion.
If there are *multiple* implicit conversions possible, then the compiler
will indicate an ambiguous implicit conversion situation.
HTH,
--Eljay
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: temporary auto_ptr
2005-09-29 12:26 ` John Love-Jensen
@ 2005-09-29 12:35 ` Dima Sorkin
2005-09-29 13:34 ` John Love-Jensen
0 siblings, 1 reply; 6+ messages in thread
From: Dima Sorkin @ 2005-09-29 12:35 UTC (permalink / raw)
To: John Love-Jensen; +Cc: gcc-help
> If there is a *single* implicit conversion possible, then I think you'll get
> that implicit conversion.
>
> If there are *multiple* implicit conversions possible, then the compiler
> will indicate an ambiguous implicit conversion situation.
At least I don't see any options for multiple implicit conversion here.
It seems to be one and single .
I don't see neither multiple paths to do the conversion,nor sequence of
conversions that require more than one cast.
(maybe there are some hidden things in actual implementation of auto_ptr,
however)
Regards,
Dima.
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: temporary auto_ptr
2005-09-29 12:35 ` Dima Sorkin
@ 2005-09-29 13:34 ` John Love-Jensen
0 siblings, 0 replies; 6+ messages in thread
From: John Love-Jensen @ 2005-09-29 13:34 UTC (permalink / raw)
To: Dima Sorkin; +Cc: MSX to GCC
Hi Dima,
> At least I don't see any options for multiple implicit conversion here. It
seems to be one and single.
I see three implicit conversions.
std::auto_ptr<const A>::auto_ptr(std::auto_ptr_ref<const A>)
... via the operator cast
std::auto_ptr<const A>::auto_ptr(std::auto_ptr<const A>&)
... by template constructor
std::auto_ptr<const A>::auto_ptr(std::auto_ptr<const A>&)
... by copy constructor
The "perfect match" would be...
std::auto_ptr<const A>::auto_ptr(std::auto_ptr<const A>)
... but that does not exist.
> I don't see neither multiple paths to do the conversion,nor sequence of
conversions that require more than one cast.
See above.
The real issue is that a std::auto_ptr<const B> is *NOT* a
std::auto_ptr<const A>, since std::auto_ptr<const B> is *NOT* derived from
std::auto_ptr<const A>. The former is not interchangeable with the latter.
HTH,
--Eljay
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2005-09-29 13:34 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-09-29 11:53 temporary auto_ptr Dima Sorkin
2005-09-29 12:11 ` John Love-Jensen
2005-09-29 12:23 ` Dima Sorkin
2005-09-29 12:26 ` John Love-Jensen
2005-09-29 12:35 ` Dima Sorkin
2005-09-29 13:34 ` John Love-Jensen
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).