Topic: auto_ptr questions
Author: Emmanuel Lesueur <lesueur@desargues.univ-lyon1.fr>
Date: 1999/05/25 Raw View
Hello,
here are a few questions concerning auto_ptrs:
1) I read Scott Meyer's article on auto_ptr at
http://www.awl.com/cseng/titles/0-201-63371-X/auto_ptr.html,
and I don't agree with the discussion of the derived-to-base
direct-initialization case:
struct Base {};
struct Derived : Base {};
auto_ptr<Derived> source();
auto_ptr<Base> p( source() );
The GOTW #25 quoted in this article says that there is one viable
constructor:
auto_ptr<Base>::auto_ptr(auto_ptr_ref<Base>),
callable using the conversion
auto_ptr<Derived>::operator auto_ptr_ref<Base>().
However, it seems to me that the constructor actually is
auto_ptr<Base>::auto_ptr(auto_ptr<Base>::auto_ptr_ref<Base>),
while the conversion operator is
auto_ptr<Derived>::operator auto_ptr<Derived>::auto_ptr_ref<Base>().
auto_ptr<Base>::auto_ptr_ref<Base> and auto_ptr<Derived>::auto_ptr_ref<Base>
are different (and unrelated) types, right ?
If so, that means that there is no viable constructor, and that
direct-initialization from an auto_ptr of derived class is ill-formed.
For that trick to work, the auto_ptr_ref member template, should be
a member of a base class common to all auto_ptrs.
2) Consider:
struct A{};
auto_ptr<A> source();
auto_ptr<A> p;
p=source();
Since source() returns an rvalue, and operator='s parameter is a
reference to non-const, this is ill-formed. Was a operator=(auto_ptr_ref<X>)
forgotten ?
3) 20.4.5.3 does not require Y* to be implicitly convertible to X*
in the specification of
template<class Y> operator auto_ptr<Y>().
But if an implementation makes the casts explicit, a compiler will
silently accept copy-initialization from base class to derived class.
This certainly is not the intent. Did I miss something ?
--
Emmanuel Lesueur - lesueur@desargues.univ-lyon1.fr
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]