Help: auto_ptr conversion confusion

Martin Sebor sebor@roguewave.com
Sat Oct 12 12:40:00 GMT 2002


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



More information about the Gcc-bugs mailing list