This is the mail archive of the gcc-bugs@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]

Re: Help: auto_ptr conversion confusion


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


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