<boost/function.hpp>
synopsis Here MAX_ARGS
is an implementation-defined constant that defines the maximum number of function arguments supported by Boost.Function and will be at least 10. The MAX_ARGS
constant referred to in this document need not have any direct representation in the library.
namespace boost { class function_base // Deprecated { bool empty() const; }; // For N in [0, MAX_ARGS] template<typename Signature, typename Arg1, typename Arg2, ... typename ArgN, typename Allocator = std::allocator<void> > class functionN : public function_base { typedef implementation-defined safe_bool; public: typedef ResultType result_type; // [1] typedef Allocator allocator_type; typedef Arg1 argument_type; // If N == 1 typedef Arg1 first_argument_type; // If N == 2 typedef Arg2 second_argument_type; // If N == 2 typedef Arg1 arg1_type; typedef Arg2 arg2_type; . . . typedef ArgN argN_type; enum { arity = N }; // Construction explicit functionN(); functionN(const functionN&); template<typename F> functionN(F); template<typename F> functionN(reference_wrapper<F>); // Assignment functionN& operator=(const functionN&); template<typename F> functionN& operator=(F); template<typename F> functionN& operator=(reference_wrapper<F>); void swap(functionN&); void clear(); // Boolean context operator safe_bool() const; bool operator!() const; // Invocation result_type operator()(Arg1 a1, Arg2 a2, ..., ArgN aN) const; }; template<typename ResultType, typename Arg1, typename Arg2, ... typename ArgN, typename Allocator> void swap(functionN<ResultType, Arg1, Arg2, ..., ArgN, Allocator>&, functionN<ResultType, Arg1, Arg2, ..., ArgN, Allocator>&); // For any N in [0, MAX_ARGS] template<typename Signature, // Function type: ResultType (Arg1, Arg2, ..., ArgN) typename Allocator = std::allocator<void> > class function : public functionN<ResultType, Arg1, Arg2, ..., ArgN, Allocator> { // Construction function(); function(const function&); function(const functionN<ResultType, Arg1, Arg2, ..., ArgN, Allocator>&); template<typename F> functionN(F); // Assignment function& operator=(const function&); function& operator=(const functionN<ResultType, Arg1, Arg2, ..., ArgN, Allocator>&); template<typename F> function& operator=(F); }; template<typename Signature, typename Allocator> void swap(function<Signature, Allocator>&, function<Signature, Allocator>&); }
f
is compatible if for the given set of argument types Arg1
, Arg2
, ..., ArgN
and a return type ResultType
, the appropriate following function is well-formed:
// if ResultType is not void ResultType foo(Arg1 arg1, Arg2 arg2, ..., ArgN argN) { return f(arg1, arg2, ..., argN); } // if ResultType is void ResultType foo(Arg1 arg1, Arg2 arg2, ..., ArgN argN) { f(arg1, arg2, ..., argN); }
A special provision is made for pointers to member functions. Though they are not function objects, Boost.Function will adapt them internally to function objects. This requires that a pointer to member function of the form R (X::*mf)(Arg1, Arg2, ..., ArgN) cv-quals
be adapted to a function object with the following function call operator overloads:
template<typename P> R operator()(cv-quals P& x, Arg1 arg1, Arg2 arg2, ..., ArgN argN) const { return (*x).*mf(arg1, arg2, ..., argN); }
f
of type F
is stateless if it is a function pointer or if boost::is_stateless<T>
is true. The construction of or copy to a Boost.Function object from a stateless function object will not cause exceptions to be thrown and will not allocate any storage.function_base
Class function_base
is the common base class for all Boost.Function objects. Objects of type function_base
may not be created directly.
Note: the use of this class by users is deprecated. This class will become an implementation detail in the future.
true
if the function object has a target, false
otherwise.functionN
Class template functionN
is actually a family of related classes function0
, function1
, etc., up to some implementation-defined maximum. In this context, N
refers to the number of parameters and f
refers to the implicit object parameter.
f.empty()
. functionN(const functionN& g);
f
contains a copy of the g
's target, if it has one, or is empty if g.empty()
. g
throws. template<typename F> functionN(F g);
g
is a compatible function object.f
targets a copy of g
if g
is nonempty, or f.empty()
if g
is empty.g
is stateless; otherwise, may throw when a copy of g
is made. template<typename F> functionN(reference_wrapper<F> g);
g.get()
is a compatible function object.this
object targets g
(not a copy of g.get()
) if g.get()
is nonempty, or this->empty()
if g.get()
is empty. functionN& operator=(const functionN& g);
f
targets a copy of g
's target, if it has one, or is empty if g.empty()
. *this
.g
is a stateless function object or a reference to the function object. template<typename F> functionN& operator=(F g);
g
is a compatible function object.f
targets a copy of g
if g
is nonempty, or f.empty()
if g
is empty.*this
.g
is a stateless function object. template<typename F> functionN& operator=(reference_wrapper<F> g);
g.get()
is a compatible function object.f
targets g.get()
(not a copy of g.get()
) if g.get()
is nonempty, or f.empty()
if g.get()
is empty.*this
.this
throws.f
and g
. safe_bool
equivalent of !empty()
safe_bool
type can be used in contexts where a bool is expected (e.g., an if condition); however, implicit conversions (e.g., to int) that can occur with bool are not allowed, eliminating some sources of user error.
this->empty()
result_type operator()(Arg1 a1, Arg2 a2, ..., ArgN aN) const;
!empty()
.const
or volatile
qualified.
policy_type policy;
policy.precall(this);
target(a1, a2, ..., aN);
policy.postcall(this);
function
Class template function
is a thin wrapper around the numbered class templates function0
, function1
, etc. It accepts up to MAX_ARGS arguments, but when passed N arguments it will derive from functionN
specialized with the arguments it receives.
The semantics of all operations in class template function
are equivalent to that of the underlying functionN
object, although additional member functions are required to allow proper copy construction and copy assignment of function
objects.
template<typename ResultType, typename Arg1, typename Arg2, ... typename ArgN, typename Allocator> void swap(functionN<ResultType, Arg1, Arg2, ..., ArgN, Allocator>& f, functionN<ResultType, Arg1, Arg2, ..., ArgN, Allocator>& g);
f.swap(g);
template<typename Signature, typename Allocator> void swap(function<Signature, Allocator>& f, function<Signature, Allocator>& g);
f.swap(g);
[1] On compilers not supporting void returns, when the ReturnType
is void, the result_type
of a Boost.Function object is implementation-defined.