Boost logo

Boost Users :

Subject: Re: [Boost-users] [shared_ptr] Can the custom deleter be a simple function?
From: Lars Viklund (zao_at_[hidden])
Date: 2011-11-06 18:44:42


On Sun, Nov 06, 2011 at 12:17:01PM -0900, Dave Abrahams wrote:
>
> on Sun Nov 06 2011, Robert Jones <robertgbjones-AT-gmail.com> wrote:
>
> > The shared_ptr docs suggest that a smart pointer employing a null
> > deleter can be
> > implemented using a functor. Why prefer a functor to a function?
>
> Two reasons I can think of:
>
> - Function objects can have a templated operator(), so you don't need to
> write a new one for each T in shared_ptr<T>.
>
> - One less level of function call indirection (very minor win)

Let's not forget a very strong reason - functors can contain arbitrary
state, as that provided when binding arguments to a callable with
Boost.Bind, or any user-defined functor or stateful lambda.

A naked free function pointer should really not appear in a modern C++
interface, it's something you expect to see in a C-style API, with some
void* blob of "context" passed in if very lucky.

-- 
Lars Viklund | zao_at_[hidden]

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net