Article View: comp.std.c++
Article #3932Re: sample auto_ptr template
From: Ajay Kamdar
Date: Sat, 06 Apr 1996 05:35
Date: Sat, 06 Apr 1996 05:35
59 lines
2574 bytes
2574 bytes
In article <bill-0504961003150001@bgibbons.vip.best.com>,
Bill Gibbons <bill@gibbons.org> wrote:
>
>Transfer of ownership is not the end goal - the end goal is
>to make auto_ptr useful for the "resource acquisition is
>initialization" idiom. That is very painful without transfer
>of ownership.
>
>In particular, if you want to do the resource acquisition in a
>function called by the function which needs to hold the resource,
>there is no good exception-safe way to pass the pointer from the
>callee to the caller.
>
>You can get close (at some cost in clarity):
>
[ snip... ]
It is not clear at all that the copy semantics of auto_ptr
are essential for exception-safe transfer of resources.
The same example coded as follows does not use
the copy semantics of auto_ptr:
extern X* get_X(); // returns a resource acquired
// by the callee, to be deleted
// by the caller.
void f() {
auto_ptr<X> ptr = get_X();
// resource allocated by get_X()
...
}
And get_X() is not unnecessarily complicated either:
X* get_X()
{
auto_ptr<X> p = new X;
// ... stuff that could throw an exception
// We got here. Means normal return.
return p.release();
}
What's wrong with this? It doesn't require copy semantics
for auto_ptr. Yet both the caller and the callee
are exception safe and there is no loss of clarity.
--
Ajay Kamdar | Email: ajay@lehman.com | Standard Disclaimer
Lehman Brothers | Phone: (201) 524-5048 |
---
[ 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:
<4k4noe$igl@jabba.lehman.com>Path:
arc1-ca.pugleaf.net!in1.uu.net!bounce-backReferences:
<009A04DA6A831C40.49800EAC@ittpub.nl> <4k0m72$gm1@jabba.lehman.com> <bill-0504961003150001@bgibbons.vip.best.com>Show Full Headers
Complete Article Headers
From: ajay@lehman.com (Ajay Kamdar) Message-ID: <4k4noe$igl@jabba.lehman.com> X-Original-Date: 5 Apr 1996 22:17:34 -0500 Path: arc1-ca.pugleaf.net!in1.uu.net!bounce-back Date: 06 Apr 96 05:35:59 GMT Approved: fjh@cs.mu.oz.au Newsgroups: comp.std.c++ Subject: Re: sample auto_ptr template Organization: Lehman Brothers, Inc. References: <009A04DA6A831C40.49800EAC@ittpub.nl> <4k0m72$gm1@jabba.lehman.com> <bill-0504961003150001@bgibbons.vip.best.com> X-Auth: PGPMoose V1.1 PGP comp.std.c++ iQBFAgUBMWYCyeEDnX0m9pzZAQE1aAF8D7WOFVCx6rLwgcci+7TFxQL1ptgaXFtt cNk6P5/izYomL79x2Y6bs4eAPIVm6b4k =4Vwz