temporary auto_ptr

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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 of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux