This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
temporary auto_ptr
- From: Dima Sorkin <dima dot sorkin at gmail dot com>
- To: gcc-help at gcc dot gnu dot org
- Date: Thu, 29 Sep 2005 13:53:00 +0200
- Subject: temporary auto_ptr
- Reply-to: Dima Sorkin <dima dot sorkin at gmail dot com>
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.