Article View: comp.std.c++
Article #3903Re: sample auto_ptr template
From: Wil Evers
Date: Wed, 03 Apr 1996 14:38
Date: Wed, 03 Apr 1996 14:38
69 lines
3204 bytes
3204 bytes
Hi,
In article <gregorDp23ts.Axt@netcom.com> gregor@netcom.com (Greg Colvin)
writes:
> 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.
>
> [snip]
>
> 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; }
> };
If I don't misunderstand the above implementation, it does much more
than just allow auto_ptrs to be returned from functions. It also
allows auto_ptrs to be dereferenced after the ownership of the pointee
has been transferred to another auto_ptr.
Is this intentional? Did the committee explicitly decide to allow
dereferencing of non-owning auto_ptrs? If so, what is the rationale behind
this?
It seems to me that after the ownership has been transferred, the
no-longer-owning auto_ptr points to an object of undetermined ownership
and lifetime. In other words, its behavior is in no way different from
a built-in pointer.
The old (April 95 DWP) definition of auto_ptr at least guaranteed that if an
auto_ptr was pointing at something, it was pointing at a unique and alive
object. Now, we don't even have a way to query if this is the case.
This is a terrible state of affairs, especially when we realize
auto_ptr is the *only* smart pointer we have in the standard library.
In my opinion, it should be renamed to zombie_ptr if it is to keep its
new semantics.
- Wil
---
[ 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:
<009A04DA6A831C40.49800EAC@ittpub.nl>Path:
arc1-ca.pugleaf.net!in2.uu.net!bounce-backShow Full Headers
Complete Article Headers
From: "Wil Evers" <wil@ittpub.nl> Message-ID: <009A04DA6A831C40.49800EAC@ittpub.nl> X-Original-Date: Wed, 3 Apr 96 11:37:34 WET Path: arc1-ca.pugleaf.net!in2.uu.net!bounce-back Date: 03 Apr 96 14:38:46 GMT Approved: fjh@cs.mu.oz.au Organization: - Newsgroups: comp.std.c++ Subject: Re: sample auto_ptr template X-Vms-Mail-To: IN%"std-c++@ncar.ucar.edu" X-Auth: PGPMoose V1.1 PGP comp.std.c++ iQBFAgUBMWKNiuEDnX0m9pzZAQGJdQF9HLlV54hOVi3yapAjSoHOjFsU+YoQ7cqu OzcIFKBlG37l4oDXyA1/913snLfNBKpY =4Cxs