C++11 three-Lamda expression of learning notes, Std::function, Std::bind

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.