Topic: auto_ptr and possible bug in standard?
Author: Kevin_VanHorn@ndsu.nodak.edu (Kevin S. Van Horn)
Date: Thu, 17 Oct 2002 01:26:48 +0000 (UTC) Raw View
The design of auto_ptr<T> is supposed to allow objects of this class
to be returned from a function, while making all "copies" actually
"moves". I've been reading the Standard lately, and either I'm
misreading or missing something in the Standard, or it doesn't
actually permit the desired behavior of auto_ptr<T>!
Section 12.8, paragraph 1 of the Standard states
"A class object can be copied in two ways, by initialization (12.1,
8.5), including for function argument passing (5.2.2) and for function
value return (6.6.3), and by assignment (5.17). Conceptually, THESE
TWO OPERATIONS ARE IMPLEMENTED BY A COPY CONSTRUCTOR (12.1) and COPY
ASSIGNMENT OPERATOR (13.5.3)."
(Emphasis added.) Thus, as I read the above, a copy constructor is
always used to return a value from a function. Section 12.1, paragraph
10 defines a copy ctor:
"A copy constructor for a class X is a constructor with a first
parameter of type X& or of type const X&."
This implies that to return an rvalue of type X from a function, or to
pass an rvalue as a function argument that is to be of type X, a
constructor X(const X&) must exist and be accessible, since a copy
ctor is used to return or pass the arguments. This ctor must take X&
or X const &, and the X& form cannot be used with temporaries.
But auto_ptr<T> doesn't have a ctor taking auto_ptr<T> const &!
Instead, it has a ctor taking an auto_ptr_ref, and a conversion to
auto_ptr_ref. A return of an auto_ptr value is supposed to happen by
converting the auto_ptr to auto_ptr_ref, then using this to initialize
the temporary for the return value.
If my analysis is wrong, can someone explain to me where, preferably
citing the appropriate section and paragraph of the Standard?
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: rani_sharoni@hotmail.com ("Rani Sharoni")
Date: Thu, 17 Oct 2002 18:00:57 +0000 (UTC) Raw View
"Kevin S. Van Horn" <Kevin_VanHorn@ndsu.nodak.edu> wrote in message
news:a2872e97.0210161721.7ee1b1d0@posting.google.com...
> The design of auto_ptr<T> is supposed to allow objects of this class
> to be returned from a function, while making all "copies" actually
> "moves". I've been reading the Standard lately, and either I'm
> misreading or missing something in the Standard, or it doesn't
> actually permit the desired behavior of auto_ptr<T>!
>
> Section 12.8, paragraph 1 of the Standard states
>
> "A class object can be copied in two ways, by initialization (12.1,
> 8.5), including for function argument passing (5.2.2) and for function
> value return (6.6.3), and by assignment (5.17). Conceptually, THESE
> TWO OPERATIONS ARE IMPLEMENTED BY A COPY CONSTRUCTOR (12.1) and COPY
> ASSIGNMENT OPERATOR (13.5.3)."
>
> (Emphasis added.) Thus, as I read the above, a copy constructor is
> always used to return a value from a function. Section 12.1, paragraph
> 10 defines a copy ctor:
>
> "A copy constructor for a class X is a constructor with a first
> parameter of type X& or of type const X&."
>
> This implies that to return an rvalue of type X from a function, or to
> pass an rvalue as a function argument that is to be of type X, a
> constructor X(const X&) must exist and be accessible, since a copy
> ctor is used to return or pass the arguments. This ctor must take X&
> or X const &, and the X& form cannot be used with temporaries.
>
> But auto_ptr<T> doesn't have a ctor taking auto_ptr<T> const &!
> Instead, it has a ctor taking an auto_ptr_ref, and a conversion to
> auto_ptr_ref. A return of an auto_ptr value is supposed to happen by
> converting the auto_ptr to auto_ptr_ref, then using this to initialize
> the temporary for the return value.
>
> If my analysis is wrong, can someone explain to me where, preferably
> citing the appropriate section and paragraph of the Standard?
>
The real analysis is more subtle, refer:
Fixing auto_ptr - the analysis is very interesting but the DR below
suppresses some of it (case 4).
http://anubis.dkuug.dk/jtc1/sc22/wg21/docs/papers/1997/N1128.pdf
DR84: Overloading and conversion loophole used by auto_ptr
http://anubis.dkuug.dk/jtc1/sc22/wg21/docs/cwg_defects.html#84
Enjoy reading,
Rani
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]