question about auto_ptr initialization

T

timlyee

int *p = new int;
auto_ptr<int> ap1 = p; //will fail on 3 1
auto_ptr<int> ap1(p); //ok 2
*ap1 = 12; // 3

the first situation has called : explicit auto_ptr(_Ty *_Ptr = 0)
_THROW0()

the second : auto_ptr(auto_ptr_ref<_Ty> _Right) _THROW0()

why the two expression have called the different ctor?

is the 1 expression has compiled as follow?

auto_ptr<int> ap1;
ap1 = p;
 
K

kingfox

int *p = new int;
auto_ptr<int> ap1 = p; //will fail on 3 1
auto_ptr<int> ap1£¨p£©; //ok 2
*ap1 = 12; // 3

the first situation has called : explicit auto_ptr(_Ty *_Ptr = 0)
_THROW0()

the second : auto_ptr(auto_ptr_ref<_Ty> _Right) _THROW0()

why the two expression have called the different ctor?
Are you sure you can compile above code correctly? When I try to
compile the above code, the compiler(MinGW gcc 4.1.2) said "error:
conversion from 'int*' to non-scalar type 'std::auto_ptr<int>'
requested" at "auto_ptr said:
is the 1 expression has compiled as follow?

auto_ptr<int> ap1;
ap1 = p;
I think so. And I remember that auto_ptr doesn't overload the '='
operator for a pointer.
 
J

James Kanze

int *p = new int;
auto_ptr<int> ap1 = p; //will fail on 3 1
auto_ptr<int> ap1(p); //ok 2
*ap1 = 12; // 3
the first situation has called : explicit auto_ptr(_Ty *_Ptr = 0)
_THROW0()

According to the standard, the line labeled 1 first converts the
argument to the target type, then copies. Since the one
argument constructor is marked explicit, it can't be used for
conversion; in fact, there is no implict conversion, and line
one should fail to compile. (G++ says: "error: conversion from
'int*' to non-scalar type 'std::auto_ptr<int>' requested")

Many of the earlier versions of auto_ptr allowed it, so if you
have an older compler, it might compile; some newer ones might
allow it as well, to avoid breaking older code.
the second : auto_ptr(auto_ptr_ref<_Ty> _Right) _THROW0()

Nothing in your code should call this. The line labeled 2 calls
st::auto_ptr<int>( int* ), exactly as it looks like. In this
case, no conversion is involved, so the "explicit" doesn't
inhibit anything.
why the two expression have called the different ctor?

Because they have different semantics. The statement labeled 1
is "copy initialization", and the statement labeled 2 is "direct
initialization". Although the exact details are a bit more
complex, the basic principle is that copy initialization
converts the initialization to the target type, then uses the
copy constructor to copy it. (The additional temporary, and
thus the copy constructor, may be elided, but the legality of
the code may not depend on this elision.) Direct initialization
does exactly what it's name says, it calls the constructor for
the object with the given arguments.
is the 1 expression has compiled as follow?
auto_ptr<int> ap1;
ap1 = p;

Never. Despite the =, copy initialization is *not* assignment,
and does not involve assignment. (Of course, this won't compile
with auto_ptr either, since there is no assignment operator
which takes a pointer. But in earlier versions, there was, so
your milage may vary.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top