转发自Queena为learning circle准备的Blog. 原文地址:C++11 Functional
##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 |
|
std::bad_function_call is the type of the exception thrown by std::function::operator() if the function wrapper has no target.
example:
1 | std::function<int()> f = nullptr; |
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 | struct Foo { |
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 | int info(std::string& name, int age, double high, double weight) { |
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 | void fun(int& n1, int& n2, const int& n3) { |
output:
1 | Before: 1 2 3 |
Reference: std::ref/cref