diff --git a/faq.html b/faq.html index 73e0305..d922646 100644 --- a/faq.html +++ b/faq.html @@ -35,7 +35,7 @@ void g() { return f(); }
In November and December of 2000, the issue of cloning vs. reference counting was debated at length and it was decided that cloning gave more predictable semantics. I won't rehash the discussion here, but if it cloning is incorrect for a particular application a reference-counting allocator could be used.
Member function assignments are not included directly in boost::function
because they do not conform to the syntax of function objects. Several libraries exist to wrap member functions in a function object and/or bind the first argument to the member function (the this
pointer). A few libraries are Boost.Function documentation.
+
Member function assignments are not included directly in boost::function
because they do not conform to the syntax of function objects. Several libraries exist to wrap member functions in a function object and/or bind the first argument to the member function (the this
pointer). A few libraries are described in the Boost.Function documentation.
Generally, any place in which a function pointer would be used to defer a call or make a callback, Boost.Function can be used instead to allow the user greater flexibility in the implementation of the target. Targets can be any 'compatible' function object (or function pointer), meaning that the arguments to the interface designated by Boost.Function can be converted to the arguments of the target function object.
<boost/function.hpp>
synopsisfunction_base
functionN
function
function
family<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 + { + bool empty() const; + operator bool() const; + }; + + // For N in [0, MAX_ARGS] + template<typename ResultType, + typename Arg1, + typename Arg2, + ... + typename ArgN, + typename Policy = empty_function_policy, + typename Mixin = empty_function_mixin, + typename Allocator = std::allocator<function_base> > + class functionN : public function_base, public Mixin + { + typedef ResultType result_type; + typedef Policy policy_type; + typedef Mixin mixin_type; + 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 + + // Construction + functionN(); + functionN(const functionN&); + template<typename F> functionN(const F&); + + // Assignment + functionN& operator=(const functionN&); + template<typename F> functionN& operator=(const F&); + void set(const functionN&); + template<typename F> void set(const F&); + void swap(functionN&); + void clear(); + + // Invocation + result_type operator()(Arg1 a1, Arg2 a2, ..., ArgN aN); + result_type operator()(Arg1 a1, Arg2 a2, ..., ArgN aN) const; + }; + + template<typename ResultType, + typename Arg1, + typename Arg2, + ... + typename ArgN, + typename Policy, + typename Mixin, + typename Allocator> + inline void swap(const function<Arg1, Arg2, ...ArgN, Policy, Mixin, Allocator>&, + const function<Arg1, Arg2, ...ArgN, Policy, Mixin, Allocator>&); + + // For any N in [0, MAX_ARGS] + template<typename ResultType, + typename Arg1, + typename Arg2, + ... + typename ArgN, + typename ArgN+1 = implementation-defined, + typename ArgN+2 = implementation-defined, + ... + typename ArgMAX_ARGS = implementation-defined> + class function : public functionN<Arg1, Arg2, ..., ArgN> + { + // Construction + function(); + function(const function&); + functionN(const functionN&); + template<typename F> functionN(const F&); + + // Assignment + function& operator=(const function&); + functionN& operator=(const functionN&); + template<typename F> function& operator=(const F&); + void set(const function&); + void set(const functionN&); + template<typename F> void set(const F&); + }; + + template<typename ResultType, + typename Arg1, + typename Arg2, + ... + typename ArgMAX_ARGS> + inline void swap(const function<Arg1, Arg2, ...ArgMAX_ARGS>&, + const function<Arg1, Arg2, ...ArgMAX_ARGS>&); +} ++ +
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.
+
+
bool empty() const
+
true
if the function object has a target, false
otherwise. operator bool() const
+
!empty()
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.
+
+
functionN();
+
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()
. The mixin for the f
is copy-constructed from the mixin of g
. template<typename F> functionN(const F& g);
+
g
is a compatible function object.f
targets a copy of g
.g
is a reference-to-const
because it is a portable, efficient, and concise way to accept any function object or function pointer. In the case of a function pointer, the type of g
is reference-to-const
pointer-to-function. functionN& operator=(const functionN& g);
+
f
targets a copy of g
's target, if it has one, or is empty if g.empty()
. The mixin for f
is assigned the value of the mixin for g
.*this
. template<typename F> functionN& operator=(const F& g);
+
g
is a compatible function object.f
targets a copy of g
.*this
.g
is a reference-to-const
because it is a portable, efficient, and concise way to accept any function object or function pointer. In the case of a function pointer, the type of g
is reference-to-const
pointer-to-function. void set(const functionN& g);
+
*this = g
. template<typename F> void set(const F&);
+
*this = g
. void swap(functionN& g);
+
f
and g
. void clear();
+
!empty()
, deallocates current target.empty()
. result_type operator()(Arg1 a1, Arg2 a2, ..., ArgN aN);
+
!empty()
.const
or volatile
qualified.
+ policy_type policy;
policy.precall(this);
target(a1, a2, ..., aN);
policy.postcall(this);
result_type operator()(Arg1 a1, Arg2 a2, ..., ArgN aN) const;
+
!empty()
.const
qualified but not volatile
qualified.
+ policy_type policy;
policy.precall(this);
const-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 Policy, + typename Mixin, + typename Allocator> +inline void swap(const function<Arg1, Arg2, ...ArgN, Policy, Mixin, Allocator>& f, + const function<Arg1, Arg2, ...ArgN, Policy, Mixin, Allocator>& g); ++
f.swap(g);
+
+template<typename ResultType, + typename Arg1, + typename Arg2, + ... + typename ArgMAX_ARGS> +inline void swap(const function<Arg1, Arg2, ...ArgMAX_ARGS>& f, + const function<Arg1, Arg2, ...ArgMAX_ARGS>& g); ++
f.swap(g);
A wrapper is defined simply by specializing a function
object with the desired return type and argument types. Any number of arguments may be supplied, up to some implementation-defined limit (10 is the default maximum). The following declares a function object wrapper f
that takes two int
parameters and returns a float
:
boost::function<float, int, int> f; @@ -99,8 +349,7 @@ object. Handling argument binding is beyond the scope of Boost.Function. However
function
familyfunction
family The header <boost/function.hpp> defines the primary entry point to the function object wrappers, the class template boost::function
. This class template is essentially a thin wrapper around a set of similar numbered function object wrappers, boost::function0
, boost::function1
, etc., where the number indicates the number of arguments passed to the function object target. The declaration of f
above could also be written as:
boost::function2<float, int, int> f; @@ -108,65 +357,9 @@ boost::function2<float, int, int> f;The numbered class templates contain most of the implementation and are each distinct class templates. They may be helpful if used in shared libraries, where the number of arguments supported by Boost.Function may change between revisions. Additionally, some compilers (e.g., Microsoft Visual C++ 6.0) have been known to be incapable of compiling
boost::function
in some instances but are able to handle the numbered variants. -Operations on function object wrappers
- -Each function object wrapper type (that has N actual arguments) supports the following operations: -
Syntax | -Semantics | -
---|---|
-f = func_obj; -f.set(func_obj); |
- Clears out f 's current target and retargets f to a copy of func_obj . |
-
-f.clear(); |
- Removes f 's target, if it has one. |
-
-(bool)f -!f.empty() |
- The conversion to bool evaluates true if a target exists, whereas empty() returns true if no target exists. |
-
f(a1, a2, ..., aN) |
- Invoke f 's current target with the given arguments.
- |
-swap(f1, f2); -f1.swap(f2); |
- Swap the targets of f1 and f2 , which must be of the same type. No exceptions will be thrown.
- |
Additionally, function object wrappers may be default-constructed (as empty) or constructed from any compatible function object. They are copy constructible and copy-assignable. - -
All function object wrappers derive from boost::function_base
, which implements the empty()
member function and the bool
conversion. Additionally, no other class may inherit boost::function_base
, so user code may rely on the implicit base pointer conversion to determine if a type is a boost::function
type or one of its variants.
-
-
The boost::function
family supports additional customization by means of policies, mixins, and allocators. The specific usage of each of these will be explained in later sections, but they share a common problem: how to replace each default with your own version.
-
For the numbered function object wrappers, one need only specify the new classes as a template parameter in the appropriate position. The following is a general definition for each of the numbered function object wrappers: -
- template<typename Return, - typename Arg1, - typename Arg2, - ... - typename ArgN, - typename Policy = empty_function_policy, - typename Mixin = empty_function_mixin, - typename Allocator = std::allocator<function_base> - > class functionN { /* ... */ }; --
With boost::function
it is not so clear, because support for an arbitrary number of parameters means that it is impossible to specify just the last parameter, but not 5 of the parameters in between. Therefore, boost::function
doubles as a generative interface for the underlying numbered class templates that uses named template parameters. For instance, to specify both a policy and an allocator for a function object wrapper f
taking an int
and returning an int
, use:
function<int, int>::policy<MyPolicy>::allocator<MyAllocator>::type f; @@ -174,8 +367,7 @@ f1.swap(f2);
The named template parameters policy
, mixin
and allocator
each take one template parameter (the replacement class) and may be nested as above to generate a function object wrapper. The ::type
at the end accesses the actual type that fits the given properties.
-
Policies define what happens directly before and directly after an invocation of a function object target is made. A policy must have two member functions, precall
and postcall
, each of which must be able to accept a const
function object wrapper pointer. The following policy will print "before" prior to execution and "after" afterwards:
@@ -189,16 +381,13 @@ struct print_policy {-Policies are further described in the Boost discussion on generic programming techniques. -
Mixins
- +Mixins
The function object wrappers allow any class to be "mixed in" as a base class. This allows extra members and/or functionality to be included by the user. This can be used, for instance, to overcome the limitations of policies by storing data between invocations in a base class instead of in a
static
member of a policy class. -Allocators
- +Allocators
The function object wrappers allow the user to specify a new allocator to handle the cloning of function object targets (when the wrappers are copied). The allocators used are the same as the C++ standard library allocators. The wrappers assume the allocators are stateless, and will create a new instance each time they are used (because they are rebound very often). This shares the semantics of most standard library implementations, and is explicitly allowed by the C++ standard. -
Example: Synchronized callbacks
- +Example: Synchronized callbacks
Synchronization of callbacks in a multithreaded environment is extremely important. Using mixins and policies, a Boost.Function object may implement its own synchronization policy that ensures that only one thread can be in the callback function at any given point in time.
We will use the prototype Boost.Threads library for its
recursive_mutex
. Since the mutex is on a per-callback basis, we will add a mutex to theboost::function
by mixin it in with this mixin class: @@ -232,17 +421,13 @@ class SynchronizedPolicy { boost::function2<float, int, int, SynchronizedPolicy, SynchronizedMixin> f;
Boost.Function has several advantages over function pointers, namely:
The above two lists were adapted from comments made by Darin Adler. -
Function object wrappers will be the size of two function pointers plus one function pointer or data pointer (whichever is larger). On common 32-bit platforms, this amounts to 12 bytes per wrapper. Additionally, the function object target will be allocated on the heap. @@ -269,15 +453,14 @@ And, of course, function pointers have several advantages over Boost.Function:
With a properly inlining compiler, an invocation of a function object requires one call through a function pointer. If the call is to a free function pointer, an additional call must be made to that function pointer (unless the compiler has very powerful interprocedural analysis). -
The function object wrappers have been designed to be as portable as possible, and to support many compilers even when they do not support the C++ standard well. The following compilers have passed all of the testcases included with boost::function
.
If your compiler is not listed, there is a small set of tests to stress the capabilities of the boost::function
library. A standards-compliant compiler should compile the code without any modifications, but if you find you run into problems the following macros can be defined to adapt the function object wrappers to a broken compiler:
-
Macro name | -Effect and symptoms | -
---|---|
BOOST_FUNCTION_USE_VIRTUAL_FUNCTIONS | -When enabled, this macro uses virtual functions instead of the default function pointers. In most cases, this will generate larger executables. However, if a compiler optimizes virtual function calls well it may result in smaller, faster executables. Enabling this macro also fixes some code generation problems in some compilers... - |
BOOST_WEAK_FUNCTION_TEMPLATE_ORDERING | -boost::function stresses function template ordering more than most compilers can handle. If your compiler is having trouble with free function pointer assignments, try defining this macro |
-
BOOST_NO_DEPENDENT_BASE_LOOKUP | -If your compiler cannot seem to find operators defined in a dependent base class (i.e., if you are trying to use boost::function operators and your compiler isn't finding them), try defining this macro |
-
BOOST_NO_DEPENDENT_NESTED_DERIVATIONS | -If your compiler can't handle the code in the function_traits_builder class, try defining this. |
-
BOOST_WEAK_CONVERSION_OPERATORS | -If expressions such as !f (for a boost::function object f ) fail, try to define this. Note that this may allow some meaningless expressions to compile, such as f+4 . |
-
If your compiler is not listed, there is a small set of tests to stress the capabilities of the boost::function
library. A standards-compliant compiler should compile the code without any modifications, but if you find you run into problems please submit a bug report.
+
The use of virtual functions tends to cause 'code bloat' on many compilers. When a class contains a virtual function, it is necessary to emit an additional function that classifies the type of the object. It has been our experience that these auxiliary functions increase the size of the executable significantly when many boost::function
objects are used.
@@ -325,8 +480,7 @@ And, of course, function pointers have several advantages over Boost.Function:
A compiler with strong interprocedural analysis could significantly reduce the overhead associated with virtual function calls such that the alternative used by Boost.Function is less efficient. No compiler has yet been found where this is true, but when it does occur the BOOST_FUNCTION_USE_VIRTUAL_FUNCTIONS macro can be defined to revert to the simpler implementation based on virtual functions. -
Many people were involved in the construction of this library. William Kempf, Jesse Jones and Karl Nelson were all extremely helpful in isolating an interface and scope for the library. John Maddock managed the formal review, and many reviewers gave excellent comments on interface, implementation, and documentation.