On Sun, Nov 06, 2011 at 12:17:01PM -0900, Dave Abrahams wrote:
on Sun Nov 06 2011, Robert Jones
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@acc.umu.se