欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

c++ lamda 函数 博客分类: c++  

程序员文章站 2024-03-19 10:56:04
...
// lamda.h
#include<iostream>
#pragma once
using namespace std;
void funcExt();
class Lamb
{
private:
    int aa =20;
    int bb =30;
public:
    Lamb();
    ~Lamb();
    void show();
    void f()
    {
        auto fun10 = [this]()
        {
            this->show();
        };
        fun10();
    }
};

 

//lamda_ext.cpp
#include "lamda.h"
auto func =[]()
{
    cout<<"funcExt result."<<endl;
};
void funcExt()
{
    func();
}
Lamb::Lamb() {};
Lamb::~Lamb() {};
void Lamb::show()
{
    cout<<aa+bb<<endl;
}

 

//lamda.cpp
#include <iostream>
#include <typeinfo>
#include "lamda.h"
int ext = 10 ;
int main()
{
    // normal
    auto func1 = []()
    {
        cout << "func1:hello world." << endl;
    };
    func1();
    // var func1 type Z4mainE3$_1 ,only auto

    // ignore params
    auto func2 = [] { cout << "func2:haha." << endl; };
    func2();

    // spec ret
    auto func3 = []() -> int { return 1.0; };
    cout << "func3 ret:" << typeid(func3()).name() << "=" << func3() << endl;

    // mutil return spec ret
    auto func4 = [](int i) -> int { if(i>10) return 1; else return 2.3; };
    cout << "func4 ret:" << typeid(func4(5)).name() << "=" << func4(5) << endl;


    funcExt();

    // normal para_list lambda,compared to func9() and func7()
    int a=5,b=6;
    auto func5 = [](int& x,int& y)
    {
        x++,y++;
        return x+y;
    };
    cout << "func5 sum:" << func5(a,b) << " a:" << a << " b:" << b << endl;

    // capture
    // capture opt: -- only capture non-static var
    // blank 不捕获任何变量
    // = 捕获外部作用域所有自动变量,按值传递给函数体,且包含this捕获,如果存在对象
    // & 捕获外部作用域所有自动变量,按引用传递给函数体,且包含this捕获,如果存在对象
    // this 按引用捕获当前对象
    {
        int c=7;
        {
            int e=9;
            {
                int f=11;/*static int ext=10;*/
            }
            auto func6 = [a, c, e, ext/*f*/]()
            {
                // capture f is illegal,not in lambda scope
                // ext is static ,not captured, but capture list allowed -- warning
                cout<<"func6 a:"<<a<<" c:"<<c<<" e:"<<e<<" ext:"<<ext<</*" f:"<<f<<*/endl;
            };
            func6();
        }
        auto func7 = [=,&a]()
        {
            // capture e is illegal,not in lambda scope
            cout<<"func7 a:"<<a<<" c:"<<c<</*" e:"<<e<<*/" ext:"<<ext<<endl;
            a++;
            //c++;
        };
        // var copy ,change not access to c
        func7();
        cout<<"a++: "<<a<<endl;
        cout<<"c++: "<<c<<endl;
        auto func8 = []()
        {
            cout<</*"a:"<<a<<" c:"<<c<<" e:"<<e<<*/"func8 ext:"<<ext<<endl;
            ext =11;
        };
        func8();
        cout<<"access static ext+1: "<<ext<<endl;
        // lambda operator() is const(copy capture) , val capture non-modifiable
        auto func9 = [=]()mutable{ c++;cout<<"c: "<<c<<endl;};
        cout<<"c: "<<c<<endl;
        func9();
        cout<<"func9 c++: "<<c<<endl;
        // capture var copy
    }
    Lamb func;
    func.f();
    // diff between = and &
    auto func11 = [=](){return a;};
    cout<<"a: "<<a;
    a++;
    cout<<" func11 a++: "<<func11()<<endl;
    auto func12 = [&]()
    {
        return b;
    };
    cout<<"b: "<<b;
    b++;
    cout<<" func12 b++: "<<func12()<<endl;
}


 转自:https://blog.csdn.net/qq_31331027/article/details/80878076