对lambda使用decltype和std :: function [英] Use decltype and std::function with lambda

查看:456
本文介绍了对lambda使用decltype和std :: function的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个工程...

auto x = 4;
typedef decltype(x) x_t;
x_t y = 5;

...为什么不这样?

... so why doesn't this?

int j = 4;  
auto func = [&] (int i) { cout << "Hello: i=" << i << "  j=" << j << endl;};
typedef decltype(func) lambda_t;
lambda_t func2 = [&] (int i) { cout << "Bye: i=" << i << "  j=" << j << endl;};

...如何声明 lambda_t 手动使用std :: function?

... and how would I declare lambda_t manually using std::function?

推荐答案


...所以为什么不是这个[工作]?

... so why doesn't this [work]?

因为lambda的每个词法实例都有不同的类型。如果使用相同的字符无关紧要。

Because each lexical instance of a lambda has a different type. It does not matter if the same characters are used.


..如何使用std :: function手动声明lambda_t?

.. and how would I declare lambda_t manually using std::function?

lambda接受一个int参数,不返回任何东西...因此:

The lambda takes an int argument and does not return anything... Therefore:

typedef std::function<void(int)> lambda_t;

这篇关于对lambda使用decltype和std :: function的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆