c++11 functional
std::function
function is a template. As with other templates we’ve used, we must specify return type and argument types when we create a function type. e.g. function<int(int, int)>
- C++98 Function pointers and member function pointers
- Exact parameter/return types must be specified.
- Can’t point to nonstatic member functions.
- Can’t point to function objects.
- boost::function
- Using for function, member functions and function objects.
- Useful for callback function.
- C++11 std::function
- Using for callable entities that can be called like a function.
- Functions, function points, function references.
- Object implicitly convertible to one of those.
- Function object.
- Useful to be able to refer to any callable entity compatible with a given calling interface.
- Using for callable entities that can be called like a function.
example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
|
std::bad_function_call is the type of the exception thrown by std::function::operator() if the function wrapper has no target.
example:
1 2 3 4 5 6 |
|
Reference: std::functionn
std::mem_fn
To use function, we must supply the call signature of the member we want to call. We can, instead, let the compiler deduce the member’s type by using another library facility,mem_fn, whick like function, is defined in the functional header.Like function, mem_fn generates a callable object from a pointer to member. Unlike function, mem_fn will deduce the type of callable from the type of the pointer to member.
- C++98 std::mem_fun/mem_fun_ref
- Only can deal with member functions with one or no argument.
- You should pick between both depending on which you want to deal with pointer or reference for class object.
- Do not support smart pointer.
- boost::mem_fn
boost::mem_fn is ageneralization of the std::mem_fun/mem_fun_ref. It support member function pointers with more than one argument, and support smart poiter. - C++11 std::mem_fn
Similar with boost::mem_fn. The name is quite confusing!(mem_fun Vs mem_fn)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
|
Reference: std::mem_fn
std::bind
The bind can be thought of as a general-purpose function adaptor. It takes a callable object and generates a new callable that “adapts” the parameter list of the original object. The general form of a call to bind is:
auto _newCallable_ = bind(_Callable_, _arg_list_);
- C++98 std::bind1st/std::bind2nd
bind1st(op, value) op(value, param) and bind2nd(op, value) op(param, value). op is a binary functor, and bind1st/bind2nd actually make the binary functor to unary functor. bind1st bind the fist parameter and bind2nd bind the sencond one. Not flexible!- Bind only first or second arguments.
- Bind only one argument at a time.
- Can’t bind functions with reference parameters.
- Require adaptable function objects. i.e.ptr_fun, mem_fun, and mem_fun_ref_.
- boost::bind
boost:::bind is generalization of the c++98 std::bin1st/std::bind2nd. It supports arbitrary function objects, function, function pointer, and member function pointers, and is able to bind any argument to a specific value or route input arguments into arbitrary positions. bind does not place any requirements on the function object; in particular. Mostly it can bind 9 parameters. - c++11 std::bind
Similar with the boost::bind.- functionObject std::bind(_callableEntity, 1stArgBinding, 2ndArgBinding… nthArgBinding); There’s no limit for binding parameters’ number.
- _1 is in namespace std::placeholders.
example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
|
Reference: std::bind
std::ref/cref
Function templates ref and cref are helper functions that generate an object of type std::reference_wrapper, using template argument deduction to determine the template argument of the result.
example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
output:
1 2 3 |
|
Reference: std::ref/cref