• C++匿名函数lambda详解


    一、匿名函数的基本语法

    [捕获列表](参数列表) mutable(可选) 异常属性 -> 返回类型 {
    	// 函数体
    }
    
    • 1
    • 2
    • 3

    语法规则:lambda表达式可以看成是一般函数的函数名被略去,返回值使用了一个 -> 的形式表示。唯一与普通函数不同的是增加了“捕获列表”。

    // lambda_test lambda_test.cc
    #include 
    
    using namespace std;
    
    void test01()
    {
    	cout << "test01" << endl;
    	auto Add = [](int a, int b) -> int {
    		return a + b;
    	};
    
    	cout << Add(1, 2) << endl;
    }
    
    int main(int argc,char **argv)
    {
    	test01();
    
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    编译(要指定-std=c++11):

    g++ -o lambda_test lambda_test.cc -std=c++11
    
    • 1

    输出结果:

    $ ./lambda_test
    test01
    3
    
    • 1
    • 2
    • 3

    一般情况下,编译器可以自动推断出lambda表达式的返回类型,所以我们可以不指定返回类型,即:

    // lambda_test lambda_test.cc
    #include 
    
    using namespace std;
    
    void test02()
    {
    	cout << "test02" << endl;
    	auto Add = [](int a, int b){
    		return a + b;
    	};
    
    	cout << Add(1, 2) << endl;
    }
    
    int main(int argc,char **argv)
    {
    	//test01();
    	test02();
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    但是如果函数体内有多个return语句时,编译器无法自动推断出返回类型,此时必须指定返回类型。

    二、捕获列表

    有时候,需要在匿名函数内使用外部变量,所以用捕获列表来传递参数。根据传递参数的行为,捕获列表可分为以下几种:

    2.1、值捕获

    与参数传值类似,值捕获的前提是变量可以拷贝,不同之处则在于,被捕获的变量在 lambda表达式被创建时拷贝,而不是在调用时才拷贝:

    // lambda_test lambda_test.cc
    #include 
    
    using namespace std;
    
    void test03()
    {
    	cout << "test03" << endl;
    	int c = 20;
    	int d = 30;
    	auto Add = [c,d](int a, int b) {
    		cout << "d = "<< d << endl;
    		return c;
    	};
    
    	d = 10; // 在这里修改 d 的值,会改变 Add里的 d 值吗?
    
    	cout << Add(1, 2) << endl;
    }
    
    
    int main(int argc,char **argv)
    {
    
    	//test01();
    	//test02();
    	
    	test03();
    	
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    执行结果:

    $ ./lambda_test
    test03
    d = 30
    20
    
    • 1
    • 2
    • 3
    • 4

    2.2、引用捕获

    与引用传参类似,引用捕获保存的是引用,值会发生变化。

    #include 
    
    using namespace std;
    void test04()
    {
    	cout << "test04" << endl;
    	int c = 20;
    	int d = 30;
    	auto Add = [c, &d](int a, int b) {
    		cout << "c = " << c << endl;
    		cout << "d = " << d << endl;
    		return c;
    	};
    
    	d = 10;//在这里修改d的值,会改变Add里的d值吗?
    
    	cout << Add(1, 2) << endl;
    }
    
    
    int main(int argc,char **argv)
    {
    
    	//test01();
    	//test02();
    	//test03();
    	test04();
    
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    执行结果:

    $ ./lambda_test
    test04
    c = 20
    d = 10
    20
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    2.3、隐式捕获

    手动书写捕获列表有时候是非常复杂的,这种机械性的工作可以交给编译器来处理,这时候可以在捕获列表中写一个 & 或 = 向编译器声明采用引用捕获或者值捕获。编译器会将外部变量全部捕获。

    #include 
    
    using namespace std;
    
    void test05()
    {
    	cout << "test05" << endl;
    	int c = 20;
    	int d = 30;
    	auto Add = [&](int a, int b) {
    		cout << "c = " << c << endl;
    		cout << "d = " << d << endl;
    		return c;
    	};
    
    	d = 10;//在这里修改d的值,会改变Add里的d值吗?
    
    	cout << Add(1, 2) << endl;
    }
    
    void test06()
    {
    	cout << "test06" << endl;
    	int c = 20;
    	int d = 30;
    	auto Add = [=](int a, int b) {
    		cout << "c = " << c << endl;
    		cout << "d = " << d << endl;
    		return c;
    	};
    
    	d = 10;//在这里修改d的值,会改变Add里的d值吗?
    
    	cout << Add(1, 2) << endl;
    }
    
    int main(int argc,char **argv)
    {
    
    	//test01();
    	//test02();
    	//test03();
    	test05();
    	test06();
    
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47

    输出:

    $ ./lambda_test
    test05
    c = 20
    d = 10
    20
    test06
    c = 20
    d = 30
    20
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    2.4、空捕获列表

    捕获列表’[]'中为空,表示Lambda不能使用所在函数中的变量。

    void test07()
    {
    	cout << "test07" << endl;
    	int c = 20;
    	int d = 30;
    	auto Add = [](int a, int b) {
    		cout << "c = " << c << endl; // 编译报错
    		cout << "d = " << d << endl; // 编译报错
    		return c;					 // 编译报错
    	};
    
    	d = 10;
    
    	cout << Add(1, 2) << endl;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    编译报错:

    lambda_test.cc:95:14: note: the lambda has no capture-default
      auto Add = [](int a, int b) {
                  ^
    lambda_test.cc:93:6: note: ‘int c’ declared here
      int c = 20;
          ^
    lambda_test.cc:97:21: error: ‘d’ is not captured
       cout << "d = " << d << endl;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    2.5、表达式捕获

    上面提到的值捕获、引用捕获都是已经在外层作用域声明的变量,因此这些捕获方式捕获的均为左值,而不能捕获右值。

    C++14之后支持捕获右值,允许捕获的成员用任意的表达式进行初始化,被声明的捕获变量类型会根据表达式进行判断,判断方式与使用 auto 本质上是相同的:

    #include 
    #include 
    using namespace std;
    
    void test08()
    {
    	cout << "test08" << endl;
    	
    	auto important = make_unique<int>(1);
    	auto Add = [v1 = 1, v2 = std::move(important)](int a, int b)->int{
    	
    		return a + b + v1 + (*v2);
    	};
    
    	cout << Add(1, 2) << endl;
    }
    
    int main(int argc,char **argv)
    {
    
    	test08();
    
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    执行结果:

    $ ./lambda_test
    test08
    5
    
    
    • 1
    • 2
    • 3
    • 4

    2.6、泛型 Lambda

    在C++14之前,lambda表示的形参只能指定具体的类型,没法泛型化。从 C++14 开始, Lambda 函数的形式参数可以使用 auto关键字来产生意义上的泛型。
    简单点说,就是通过auto使lambda自适应参数类型:

    #include 
    
    using namespace std;
    
    void test09()
    {
    	cout << "test09" << endl;
    	auto Add = [](auto a, auto b) {
    		return a + b;
    	};
    
    	cout << Add(1, 2) << endl;
    	cout << Add(1.1, 2.2) << endl;
    }
    
    int main(int argc,char **argv)
    {
    
    	test09();
    
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    执行结果:

    ./lambda_test
    test09
    3
    3.3
    
    
    • 1
    • 2
    • 3
    • 4
    • 5

    2.7、可变lambda

    (1)采用值捕获的方式,lambda不能修改其值,如果想要修改,使用mutable修饰。
    (2)采用引用捕获的方式,lambda可以直接修改其值。

    #include 
    
    using namespace std;
    
    void test10()
    {
    	cout << "test10" << endl;
    	int v = 10;
    	// 值捕获方式,使用mutable修饰,可以改变捕获的变量值
    	auto tes = [v]() mutable {
    		return ++v;
    	};
    
    	v = 5;
    	auto a = tes();// a=11;
    	cout << a << endl;
    }
    
    void test11()
    {
    	cout << "test11" << endl;
    	int v = 10;
    	auto Add = [&v]{
    		return v++;
    	};
    	v = 6;
    	cout << Add() << endl;
    }
    
    int main(int argc,char **argv)
    {
    
    	test10();
    	test11();
    
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

    执行结果:

    $ ./lambda_test
    test10
    11
    test11
    6
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    2.8、混合捕获

    1. 要求捕获列表中第一个元素必须是隐式捕获(&或=)。
    2. 混合使用时,若隐式捕获采用引用捕获(&)则显式捕获的变量必须采用值捕获的方式。
    3. 若隐式捕获采用值捕获(=),则显式捕获的变量必须采用引用捕获的方式。
    #include 
    
    using namespace std;
    
    void test12()
    {
    	cout << "test12" << endl;
    	
    	int c = 12;
    	int d = 30;
    	int e = 30;
    	// auto Add = [&, d, e](int a, int b)
    	auto Add = [=, &c](int a, int b) -> int {
    		c = a;
    		cout << "d=" << d << ", e=" << e << endl;
    		return c;
    	};
    	d = 20;
    	cout << Add(1, 2) << endl;
    	cout << "c:" << c << endl;
    }
    
    int main(int argc,char **argv)
    {
    
    	test12();
    
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    测试结果:

    $ ./lambda_test
    test12
    d=30, e=30
    1
    c:1
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    2.10、Lambda捕获列表总结

    捕获含义
    []空捕获列表,Lambda不能使用所在函数中的变量。
    [names]names是一个逗号分隔的名字列表,这些名字都是Lambda所在函数的局部变量。默认情况下,这些变量会被拷贝,然后按值传递,名字前面如果使用了&,则按引用传递
    [&]隐式捕获列表,Lambda体内使用的局部变量都按引用方式传递
    [=]隐式捕获列表,Lanbda体内使用的局部变量都按值传递
    [&,identifier_list]identifier_list是一个逗号分隔的列表,包含0个或多个来自所在函数的变量,这些变量采用值捕获的方式,其他变量则被隐式捕获,采用引用方式传递,identifier_list中的名字前面不能使用&。
    [=,identifier_list]identifier_list中的变量采用引用方式捕获,而被隐式捕获的变量都采用按值传递的方式捕获。identifier_list中的名字不能包含this,且这些名字面前必须使用&。

    总结

    1. lambda表达式的目的是把函数写的更加内聚;只需要在内部使用,就没必要写到外部,干扰其他函数,同时使代码更简洁。
    2. 如果捕获列表为[&],则表示所有的外部变量都按引用传递给lambda使用。
    3. 如果捕获列表为[=],则表示所有的外部变量都按值传递给lambda使用。
    4. 匿名函数构建的时候对于按值传递的捕获列表,会立即将当前可以取到的值拷贝一份作为常数,然后将该常数作为参数传递。

    在这里插入图片描述

  • 相关阅读:
    Java 多线程:并发编程的三大特性
    HarmonyOS-静态库(SDK)的创建和使用
    计算机网络的标准化工作与相关组织
    数字化转型导师坚鹏:数字化时代银行网点厅堂营销5大特点分析
    2023年第三届智能制造与自动化前沿国际会议(CFIMA 2023)
    百数标准应用——转运隔离数字化,降低感染风险
    将网站域名访问从http升级到https(腾讯云/阿里云)
    行业追踪,2023-10-12
    为什么美客多可以测评补单?他们早已走在流量最前沿
    SkipList(跳表)
  • 原文地址:https://blog.csdn.net/Long_xu/article/details/127869979