Topic: auto_ptr<Derived>::operator auto_ptr_ref<Base>


Author: "Sebastian Moleski" <sebmol@gmx.net>
Date: 2000/04/26
Raw View
If you want to see some real-life implementations of the STL classes,
look into the STL source that comes with the free Borland C++
compiler. It is one of the most standard-conforming compiler out so
far. Of couse, you can't just copy it but you can use it as an
inspriation for your own work. Here's the link:

http://www.borland.com/bcppbuilder/freecompiler/

--
Sebastian Moleski
----
The Borland C++ Builder bug lists:
http://www.crosswinds.net/~bcbbugs/


---
[ 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              ]






Author: Stefan <cool@worblehat.wifo.uni-mannheim.de>
Date: 2000/04/22
Raw View
I tried to implement class auto_ptr according to the actual C++ standard
(including issue 127). A test program produced a stack overflow due to
recursive calls of auto_ptr<Derived>::operator auto_ptr_ref<Base>.

An analysis of the program shows that an object of type
auto_ptr<Derived> has to be converted to auto_ptr<Base>. For this task
auto_ptr<Derived>::operator auto_ptr_ref<Base> is used. This operator
calls the constructor of auto_ptr_ref<Base> with an object of type
auto_ptr<Derived>. Here it needs the same conversion!

To work around this problem auto_ptr_ref could hold a pointer to Y
instead of a reference to an auto_ptr<Y>, like the implementation in
Josuttis book, but in this case memory leaks might occur if an
auto_ptr_ref object is created but not used.

Is there a better way to implement class auto_ptr, or should I prepare a
DR?

Thanks in advance for your suggestions!
  Stefan


//==========8<---------------------
template<class X> class auto_ptr;

template<class Y>
struct auto_ptr_ref {
 auto_ptr<Y>& p;
 explicit auto_ptr_ref(auto_ptr<Y>& a) : p(a) { }
};

template<class X>
class auto_ptr {
public:
 typedef X element_type;
 explicit auto_ptr(X* p = 0) throw() : ptr(p) { }
 template<class Y> auto_ptr(auto_ptr<Y>& a) throw()
  : ptr(a.release()) { }
 auto_ptr(auto_ptr& a) throw() : ptr(a.release()) { }
 auto_ptr(auto_ptr_ref<X> r) throw() : ptr(r.p.release()) { }
 template<class Y> auto_ptr& operator=(auto_ptr<Y>& a) throw()
  { reset(a.release()); return *this; }
 auto_ptr& operator=(auto_ptr& a) throw()
  { reset(a.release()); return *this; }
 auto_ptr& operator=(auto_ptr_ref<X> r) throw()
  { reset(r.p.release()); return *this; }
 ~auto_ptr() throw() { delete get(); }
 X& operator*() const throw() { return *get(); }
 X* operator->() const throw() { return get(); }
 X* get() const throw() { return ptr; }
 X* release() throw() { X* tmp = get(); ptr = 0; return tmp; }
 void reset(X* p = 0) throw()
  { if (get() != p) { delete get(); ptr = p; } }
 template<class Y> operator auto_ptr<Y>() throw()
  { return auto_ptr<Y>(release()); }
 template<class Y> operator auto_ptr_ref<Y>() throw()
  { return auto_ptr_ref<Y>(*this); }               // <- CRASHES HERE
private:
 X* ptr;
};

struct Base {};
struct Derived : Base {};
auto_ptr<Derived> source() { return auto_ptr<Derived>(new Derived); }
auto_ptr<Base> p2( source() );

int main() { }

---
[ 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              ]






Author: Dave Abrahams <abrahams@mediaone.net>
Date: 2000/04/26
Raw View
in article 3900B7A3.72FC84F3@wifo3.uni-mannheim.de, Stefan at
cool@worblehat.wifo.uni-mannheim.de wrote on 4/22/00 6:22 AM:

> Is there a better way to implement class auto_ptr, or should I prepare a
> DR?

The committee already has DRs on this issue. An implementation which seems
to work and to reflect the committee's original intent is available in the
STLport (see www.stlport.org). Furthermore, it has been ported to a bunch of
different compilers, so it probably works with your platform's quirks.

Regards,
Dave

---
[ 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              ]