Article View: comp.std.c++
Article #3871sample auto_ptr template
From: Greg Colvin
Date: Sat, 30 Mar 1996 06:08
Date: Sat, 30 Mar 1996 06:08
41 lines
2242 bytes
2242 bytes
In Santa Cruz we decided to change the auto_ptr copy semantics to
allow returns of auto_ptr from functions. Following is a simple
implementation. It (almost) compiles with the latest Edison Design
Group front end (the template friend declaration choked it). Note
that on most architectures the owner bit can be overlayed with the
pointer for a smaller footprint, but the following is portable.
template<class X>
class auto_ptr {
mutable bool owner;
X* px;
template<class Y> friend class auto_ptr;
public:
explicit auto_ptr(X* p=0)
: owner(p), px(p) {}
template<class Y>
auto_ptr(const auto_ptr<Y>& r)
: owner(r.owner), px(r.release()) {}
template<class Y>
auto_ptr& operator=(const auto_ptr<Y>& r) {
if ((void*)&r != (void*)this) {
if (owner)
delete px;
owner = r.owner;
px = r.release();
}
return *this;
}
~auto_ptr() { if (owner) delete px; }
X& operator*() const { return *px; }
X* operator->() const { return px; }
X* get() const { return px; }
X* release() const { owner = 0; return px; }
};
---
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Technical Details
Message-ID:
<gregorDp23ts.Axt@netcom.com>Path:
arc1-ca.pugleaf.net!in1.uu.net!bounce-backShow Full Headers
Complete Article Headers
From: gregor@netcom.com (Greg Colvin) Message-ID: <gregorDp23ts.Axt@netcom.com> X-Original-Date: Sat, 30 Mar 1996 01:22:40 GMT Path: arc1-ca.pugleaf.net!in1.uu.net!bounce-back Date: 30 Mar 96 06:08:11 GMT Approved: fjh@cs.mu.oz.au Newsgroups: comp.std.c++ Subject: sample auto_ptr template Organization: Netcom Online Communications Services (408-241-9760 login: guest) Apparently-To: comp-std-c++@uunet.uu.net X-Auth: PGPMoose V1.1 PGP comp.std.c++ iQBFAgUBMVzQOuEDnX0m9pzZAQG43gF6Aj7N03tqO/NFNt1gaIcKWpK7lAlBkaxW Rw9Sv2PLpHsl+D8x9d9ikjZpkm9hEaBn =Vwnm