可以表达 lambda 表达式的“类型"吗?

2021-12-23 00:00:00 lambda c++ c++11 stl

将 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 表达式都会创建一个新的唯一类型.如果之后您在其他地方执行以下操作,ts

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 :)

相关文章