http://blog.csdn.net/augusdi/article/details/11771699
First of all, the callback function, as I understand it, is that the function name (the address of the function) is used as a function parameter in another function.
function
#include < functional> std::function< size_t (const char*) > Print_func; std::function< size_t (const char*) > is a type, Print_func is a variable/// normal function--std::function object size_t cprint (const char*) {...} Print_func = Cprint; Print_func ("Hello World"):/// functor-Std::function object class Cxxprint {public : size_t operator () (const char*) {...} }; Cxxprint p; Print_func = p; Print_func ("Hello World");
In the example above, we assign a normal function and a functor () to a Std::function object, which we then invoke through the object. This solves the problem of type safety (why, I don't understand it)
So,std::function< size_t (const char*) > can be seen as a type of function pointer that is used to represent a function pointer, which can be called a callable entity.
The greatest use of std::function object is to implement a function callback, and the user needs to be aware that it cannot be used to check for equality or inequality
Bind
bind is a mechanism that can pre-bind certain parameters of a specified callable entity to an existing variable, producing a new callable entity that binds the function name, function parameters, and leaves several unbound (waiting for the user to pass in) to get a new callable entity
#include < functional> int Func (int x, int y); Auto Bf1 = Std::bind (Func, ten, std::p laceholders::_1); Bf1 (20); < same as Func (Ten) class A {public : int Func (int x, int y); }; A; Auto BF2 = Std::bind (&a::func, A, std::p laceholders::_1, std::p laceholders::_2); BF2 (10, 20); < same as A.func (TEN) std::function< int (int) > BF3 = Std::bind (&a::func, A, std::p laceholders::_ 1); BF3 (10); < same as A.func (10, 100)
BF2 can also be used without auto directly with std::function<int (int, int) > to declare
In the above example, BF1 is the first parameter of a two-parameter ordinary function is bound to 10, generating a new parameter of the callable body; BF2 is to bind a class member function to a class object and generate a new callable entity like a normal function; BF3 is to bind the class member function to the class object and the second parameter, generating a new Std::function object
- (1) Bind pre-bound parameters need to pass specific variables or values in, for the pre-bound parameters, is Pass-by-value
- (2) For non-binding parameters, need to pass std::p laceholders go in, starting from the _1, in turn, increment. Placeholder is pass-by-reference.
- (3) The return value of BIND is a callable entity and can be assigned directly to the Std::function object
- (4) For binding pointers, arguments of reference types, the consumer needs to ensure that these parameters are available before callable entity calls
- (5) The This of a class can be bound by an object or a pointer
Basic lambda expression syntax
#include <iostream> int main (int argc, char* argv[]) { auto func = [] () {std::cout << "Hello Wor LD "; }; Func (); return 0; }
Well, you've found the lambda expression, starting with [], this symbol becomes the capture specification , which tells us that we are creating a lambda function compilation , You will find that each LAMDA expression has such a beginning (some will be slightly different and will be introduced later).
Next, like any other function, you need a parameter list, where is the description of the return value? It turns out that we don't need to, because in the C++11 standard, the compiler can infer the return value of a lambda function, and it does not force you to add it .
The next line of Func (), which we call a lambda function, looks like any other function, and by the way, you don't need to define a function and then point to it with a function pointer.
by default, a lambda expression does not write a return statement, its default value is NULL, and if you have a simple return expression, the compiler also infers the type of the return value.
[] () { return 1;} //Compiler knows this returns an integer
If you write a more complex lambda function, with more than one return value, you must specify the return type
[] () int { return 1;} //Now we ' re telling the compiler what we want
typedef int (*FUNC) (); Func f = [] () int {return 2;}; f ();
C + + function, bind, and Lamda expressions