public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* 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

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).