C++匿名函数实现原理

匿名函数是一种函子(functor),什么是 functor ?

According to Wikipedia, A function object or usually referred to as a functor is a construct that allows an object to be called as if it were an ordinary function.

functor 是一个函数对象,其成员函数重载了 () 算符,因此其能够像普通函数调用一样调用其成员函数

使用functor相比普通函数有什么优势?

One example of the advantages of using a functor over an ordinary function is that it can access the internal member variables and functions of that object.

使用functor 比普通函数的优势在于其可以访问内部成员变量(),通常这些成员变量可以用来保存上下文(context),而使用普通函数则需要定义全局变量才可以做到。

有了functor ,为什么还需要匿名函数?

匿名函数相比显示的定义一个类并重载其() 算符,代码编写显然要简单的多,且代码可读性更强。无需繁琐的重新定义一个类。

匿名函数是如何实现的?

当我们定义一个匿名函数时,可以看成是编译器在生成代码的时候帮我们内部定义了一个类,并重载了() 算符,因此可以看成是编译器帮我做了这些工作。利用 cppinsights.io 可以查看匿名函数的实现代码

比如当值拷贝捕获时

int v = 10;
auto f = [v](){
  return v + 1;
};

其对应的编译器生成代码为

int v = 10;
    
  class __lambda_8_14
  {
    public: 
	    inline /*constexpr */ int operator()() const
	    {
	      return v + 1;
	    }
    
    private: 
	    int v;
    
    public:
	    __lambda_8_14(int & _v) : v{_v} {}
  };

可以看到当定义一个匿名函数时,编译器为其创建了一个类,并重载了() 算符,

需要注意的是上述int operator()() 默认为const类型,即无法在该函数中修改捕获的变量(成员变量),需要加上mutable关键字来去除const限制 如下

int v = 10;
auto f = [v]() mutable {
  return v++;
};

再比如引用捕获

int v = 10;
auto f = [&v, a=1](){
  return 0;
};

对应为

class __lambda_8_14 {
    public: 
	    inline /*constexpr */ int operator()() const
	    {
	      return 0;
	    }
    
    private: 
	    int & v;
	    int a;
    
    public:
	    __lambda_8_14(int & _v, const int & _a) : v{_v}, a{_a}{}
  };

如下为捕获的所有可选项

[]	Capture nothing (or, a scorched earth strategy?)
[&]	Capture any referenced variable by reference
[=]	Capture any referenced variable by making a copy
[=, &foo]	Capture any referenced variable by making a copy, but capture variable foo by reference
[bar]	Capture bar by making a copy; don't copy anything else
[this]	Capture the this pointer of the enclosing class

参考链接

  1. https://towardsdatascience.com/c-basics-understanding-lambda-7df00705fa48
  2. https://cppinsights.io/
  3. https://www.cprogramming.com/c++11/c++11-lambda-closures.html
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值