std::function class template lacks the user-specified allocator support available in its predecessor
boost::function. The current definition of
boost::function provides a second template parameter that
can be utilized to pass a custom allocator class to be used when memory allocations are needed. This second template
parameter is optional and defaults to
The main drawback of the allocator support in
boost::function is that using a different allocator type
leads to different instances of the
boost::function class template, even for functions that have the
This document proposes an alternative approach for custom allocator support for
adding a second template parameter.
The following are the types of function objects that can be used to initialize a
It is possible for
std::function implementations to avoid dynamic allocations in the first two cases.
In general, dynamic allocations can not be avoided in the third case. The current behavior of
in this case is to use
new. This precludes its use in contexts where uncontrolled dynamic allocations
are not allowed, such as embedded environments.
Note that any use of
std::function, even when the targeted object is either
pointer to function or pointer to member function, leads to the third case and the potential need for dynamic allocations.
The proposed custom allocator support is consistent with the proposed allocator support in
std::function instances retain source and binary compatibility
independently of whether user-defined allocator was used or not.
The allocator support does not add a second parameter to the
std::function class template.
It has no overhead when no allocator is used.
Even when a custom allocator is supplied, the resulting
std::function object is indistinguishable from
any other instance of the
std::function class template that use the same signature.
The proposed feature affects the interface of
std::function, allowing its broader use, and is therefore
strongly recommended to be added to the C++0x standard.
std::function the following constructor:
template <class F,class A> function( F f, A a );
f shall be callable for argument types
A shall be an allocator [allocator.requirements]. The copy constructor and destructor
A shall not throw.
!*this if any of the following hold:
NULLmember function pointer.
Fis an instance of the function class template, and
*this targets a copy of
f is not a pointer
to member function, and targets a copy of
f is a pointer to member function. A copy
a shall be used if dynamic storage must be allocated for the copy of
Implementations are encouraged to avoid the use of dynamically allocated memory for small function objects, e.g., where
f’s target is an object holding only a pointer or reference to an object and a member function pointer --
Throws: shall not throw exceptions when
f is a function pointer or a
T. Otherwise, may throw
bad_alloc or any exception thrown by
F’s copy or
std::function the following member function template:
template <class F,class A> void assign( F f, A a );
A proof of concept implementation is available at:
It is expected to be integrated with
boost::function in time for the mailing.
Thanks to Peter Dimov and Doug Gregor for reviewing this document.