Lamda
First Lamda
[]
{};
Second LAMDA
[] ()//or no Need () when paramater is null
{
Std::cout << "Second" << Std::endl;
} ();//Last Add (), express would call this LAMDA func
3 with return type
Auto KKK = [] ()
{
return 1;
}();
4, set return type
Auto KKKK = [] (int i)-bool {return (bool) I;} (5);
5, Lamda capture, [=], [=,&], [&], [this][] do not capture ...
/*[&] Capture in a referenced fashion
[=] Captured by a copy of the variable
[=, &foo] is captured by a copy of the variable, but is captured with a reference to the Foo variable
[Bar] By copying the capture, do not copy other
[This] captures the corresponding member of this pointer
*/
function, bind, etc., on the code
#include <iostream>#include<functional>using namespacestd;typedef std::function<void() >fp;typedef std::function<void(int) >fpi1;voidG_fun () {std::cout<<"global function Bind"<<Std::endl;}classa{ Public: Static voidafunstatic () {std::cout<<"afunstatic"<<Std::endl; } Virtual voidAfun () {std::cout<<"Afun"<<Std::endl; } voidAFunI1 (inti) {std::cout<<"aFunI1:"<< I <<Std::endl; }};classDeriveda: Publica{ Public: Virtual voidAfun ()Override{std::cout<<"Afun in Derivea"<<Std::endl; }};classb{ Public: voidBcallafun () {if(Callfun) callfun (); } voidbcallafunstatic () {if(callfunstatic) callfunstatic (); } voidBCallAFunI1 (inti) {if(callFunI1) callFunI1 (i); } FP callfunstatic; FP Callfun; Fpi1 callFunI1;};voidMain () {b b; A; //bind to global functionFP f = FP (&g_fun); f (); //bind to class function//StaticB.callfunstatic = Std::bind (&a::afunstatic); B.bcallafunstatic (); //no static function without parameterB.callfun = Std::bind (&a::afun, &a); B.bcallafun (); //no static function with parameterB.callfuni1 = Std::bind (&a::afuni1, &A, std::p laceholders::_1); B.bcallafuni1 (5); //About polymorphicDeriveda da; B.callfun= Std::bind (&a::afun, &da); B.bcallafun ();}
C++11 three-Lamda expression of learning notes, Std::function, Std::bind