This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
Re: Help: auto_ptr conversion confusion
- From: Martin Sebor <sebor at roguewave dot com>
- To: gcc-bugs at gcc dot gnu dot org
- Date: Sat, 12 Oct 2002 13:36:35 -0600
- Subject: Re: Help: auto_ptr conversion confusion
- Organization: Rogue Wave Software, Inc.
- References: <3DA6FC8E.3000307@framfab.dk>
- Reply-to: gcc-bugs at gcc dot gnu dot org
Kristian Spangsege wrote:
>
...
Could someone explain to me exactly what the semantical difference (if
any) is between these two versions of line 15:
15 auto_ptr<A> a = f();
15 auto_ptr<A> a(f());
The first one is referred to as copy-initialization and the second
one is direct-initialization. The rules are described in 8.5, p14
of the C++ Standard.
The difference in this case is that copy-initialization first considers
user-defined conversion sequences from the rhs to the lhs. Thus, the
auto_ptr<B> object returned from f() is first converted to auto_ptr<A>,
the target type, before it is used to initialize a. But since the
auto_ptr<A> copy ctor only converts from a non-const reference there
is no way to copy the unnamed temporary auto_ptr<A> object to a (going
through auto_ptr_ref at this point requires more user-defined
conversions than is allowed).
The direct-initialization case first considers ctors of the target
class, so it chooses the ctor auto_ptr<A>(auto_ptr_ref<B>&), since
there is an accessible user-defined conversion from auto_ptr<B> to
auto_ptr_ref<B>, and the conversion succeeds.
Regards
Martin