By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,514 Members | 1,843 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,514 IT Pros & Developers. It's quick & easy.

question about auto_ptr initialization

P: n/a
int *p = new int;
auto_ptr<intap1 = p; //will fail on 3 1
auto_ptr<intap1(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<intap1;
ap1 = p;

May 26 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On 526, 10ʱ53, timlyee <yiyem...@gmail.comwrote:
int *p = new int;
auto_ptr<intap1 = p; //will fail on 3 1
auto_ptr<intap1p; //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<intap1 = p;".
is the 1 expression has compiled as follow?

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

May 26 '07 #2

P: n/a
On May 26, 4:53 am, timlyee <yiyem...@gmail.comwrote:
int *p = new int;
auto_ptr<intap1 = p; //will fail on 3 1
auto_ptr<intap1(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<intap1;
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.)

--
James Kanze (Gabi Software) email: ja*********@gmail.com
Conseils en informatique oriente objet/
Beratung in objektorientierter Datenverarbeitung
9 place Smard, 78210 St.-Cyr-l'cole, France, +33 (0)1 30 23 00 34

May 26 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.