Boost logo

Boost :

From: Beman Dawes (bdawes_at_[hidden])
Date: 2001-04-06 12:51:24


At 12:07 PM 3/23/2001, Ronald Garcia wrote:

>I had been wondering about the purpose of scoped_ptr for a while,
>since it doesn't seem to be made very explicit in the documentation.
>short of an offhand comment about the standardization of auto_ptr. Why
>would I want to use a scoped_ptr instead of an auto_ptr? I presume it
>has something to do with the transfer of ownership semantics of
>auto_ptr. In short, I'd like to see an example of a situation where I
>would shoot myself in the foot using auto_ptr rather than scoped_ptr
>as per my usual policy of looking in the Standard Library before I
>look elsewhere. I think that's useful information for the docs.

The primary reason to use scoped_ptr rather than auto_ptr is to let readers
of your code know that you intend "resource acquisition is initialization"
to be applied only for the current scope, and have no intent to transfer
ownership.

A (very) secondary reason to use scoped_ptr is to prevent a later
maintenance programmer from adding a function that actually transfers
ownership by returning the auto_ptr (because the maintenance programmer saw
auto_ptr, and assumed ownership could safely be transferred.)

Think of bool vs int. We all know that under the covers bool is usually
just an int. Indeed, some argued against including bool in the standard
because of that. But by coding bool rather than int, you tell your readers
what your intent is. Same with scoped_ptr - you are signalling intent.

--Beman


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk