This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

temporary auto_ptr


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.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]