Can the #39;type#39; of a lambda expression be expressed?(可以表达 lambda 表达式的“类型吗?)
问题描述
将 lambda 表达式视为可调用对象的语法糖",是否可以表达未命名的基础类型?
Thinking of lambda expressions as 'syntactic sugar' for callable objects, can the unnamed underlying type be expressed?
一个例子:
struct gt {
bool operator() (int l, int r) {
return l > r;
}
} ;
现在,[](int l, int r) { return l >r;} 是对上述代码的优雅替代(加上必要的 gt 可调用对象的创建),但是有没有办法表达 gt(类型)本身?
Now, [](int l, int r) { return l > r; } is an elegant replacement for the above code (plus the necessary creation of callable objects of gt), but is there a way to express gt (the type) itself?
一个简单的用法:
std::set<int, gt> s1; // A reversed-order std::set
// Is there a way to do the same using a lambda?
std::set<int, some-magic-here-maybe([](int l, int r) { return l > r; }) > s2;
推荐答案
不,你不能把它放到 decltype 因为
No, you cannot put it into decltype because
lambda 表达式不应出现在未计算的操作数中
A lambda-expression shall not appear in an unevaluated operand
您可以执行以下操作
auto n = [](int l, int r) { return l > r; };
std::set<int, decltype(n)> s(n);
但这真的很丑.请注意,每个 lambda 表达式都会创建一个新的唯一类型.如果之后您在其他地方执行以下操作,t 与 s
But that is really ugly. Note that each lambda expression creates a new unique type. If afterwards you do the following somewhere else, t has a different type than s
auto n = [](int l, int r) { return l > r; };
std::set<int, decltype(n)> t(n);
您可以在此处使用 std::function,但请注意,这会产生一点运行时成本,因为它需要间接调用 lambda 函数对象调用运算符.在这里它可能可以忽略不计,但如果你想以这种方式将函数对象传递给 std::sort 可能很重要.
You can use std::function here, but note that this will incur a tiny bit of runtime cost because it needs an indirect call to the lambda function object call operator. It's probably negligible here, but may be significant if you want to pass function objects this way to std::sort for example.
std::set<int, function<bool(int, int)>> s([](int l, int r) { return l > r; });
和往常一样,先编写代码,然后配置文件 :)
As always, first code then profile :)
这篇关于可以表达 lambda 表达式的“类型"吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:可以表达 lambda 表达式的“类型"吗?
基础教程推荐
- CString 到 char* 2021-01-01
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 初始化列表*参数*评估顺序 2021-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
