温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

C++的lambda使用实例分析

发布时间:2022-02-18 09:06:58 来源:亿速云 阅读:95 作者:iii 栏目:开发技术

这篇“C++的lambda使用实例分析”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“C++的lambda使用实例分析”文章吧。

先说结论:

  • 对于有捕获的lambda,其等价于对象。

  • 对于没有任何捕获的lambda,其等价于函数!

首先,很多C++程序员从lambda 用法上反推容易发现是对象,因为lambda可以捕获!这是函数做不到的。的确,比如:

int n = 100;
auto foo = [n](int a) {
    return a > n;
};
cout<< foo(99);

如果编译器要实现foo,大致类比这种写法(可能真实的实现细节不是这样,但思路类似)∶

struct Foo {
    Foo(int i) {n=i;}
    bool operator()(int a) {
        return a > n;
    }
private:
    int n;
};
...
int n = 100;
Foo foo(n);
cout<< foo(99);

如果是引用捕获了变量,那么struct内有一个指针成员持有被引用捕获的变量的地址。

比如:

set<int> ns = {100, 200, 300};
auto foo = [&ns](int a) {
    return ns.find(a);
};
cout<< foo(99);

大致等价于:

struct Foo {
    Foo(set<int>* p) {p_ns = p;}
    bool operator()(int a) {
        auto &ns = *p-ns;
        return ns.find(a);
    }
private:
    set<int>* p_ns;
};
...
set<int> ns = {100, 200, 300};
Foo foo(&ns);
cout<< foo(99);

然而&hellip;&hellip;这并不是全部!
在没有捕获任何东西的时候,lambda其实是等价于普通的函数的!可以用Linux C中函数pthread_create()来验证!它只能接收一个参数是void*,返回值也是void*的回调函数。

神奇的是,无参的lambda也可以被pthread_create()使用!

using namespace std;
struct A {
    void* operator()(void*) {
        cout<<"xxxx"<<endl;
        return nullptr;
    }
};
int main() {
    A a;
    a(NULL);
    pthread_t t;
    //pthread_create(&t, NULL, a, NULL); // 编译失败
    auto cb = [](void*)->void* {
        cout<<"xxxx"<<endl;
        return nullptr;
    };
    pthread_create(&t, NULL, cb, NULL); // 编译通过
    pthread_join(t, NULL);
    return 0;
}

上面代码还可以再改一下,让cb去捕获一个变量, 比如:

auto cb = [&](void*)->void* {
        cout<<"xxxx"<<endl;
        return nullptr;
    };
    pthread_create(&t, NULL, cb, NULL);

这时,给pthread_create()传入cb同样会编译失败!错误信息:

cb.cpp: In function ‘int main()':
cb.cpp:23:30: error: cannot convert ‘main()::<lambda(void*)>' to ‘void* (*)(void*)'
   23 |     pthread_create(&t, NULL, cb, NULL);
      |                              ^~
      |                              |
      |                              main()::<lambda(void*)>
In file included from /usr/include/x86_64-linux-gnu/c++/9/bits/gthr-default.h:35,
                 from /usr/include/x86_64-linux-gnu/c++/9/bits/gthr.h:148,
                 from /usr/include/c++/9/ext/atomicity.h:35,
                 from /usr/include/c++/9/bits/ios_base.h:39,
                 from /usr/include/c++/9/ios:42,
                 from /usr/include/c++/9/ostream:38,
                 from /usr/include/c++/9/iostream:39,
                 from cb.cpp:1:
/usr/include/pthread.h:200:15: note:   initializing argument 3 of ‘int pthread_create(pthread_t*, const pthread_attr_t*, void* (*)(void*), void*)'
  200 |       void *(*__start_routine) (void *),
      |       ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~

这其实也不难理解,C++在lambda的设计上也贯彻着零开销 (Zero Overhead)原则,也就是C++不在性能上干多余的事,显然函数比对象开销更小。所以即使同为lambda,在有无捕获的时候,其底层实现其实是截然不同的!

以上就是关于“C++的lambda使用实例分析”这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI