如何将新的 lambda 表达式分配给函数对象?

How to assign a new lambda expression to a function object?

本文关键字:分配 函数 对象 表达式 lambda      更新时间:2023-10-16

已定义 lambda 表达式并将其分配/绑定到函数对象。现在我想为该函数对象分配一个新函数。但是在某些情况下,此分配会导致编译错误。

我知道错误来自auto关键字自动将const添加到函数对象。有没有办法将auto与其他关键字一起使用来删除const绑定?我真的不相信mutable可以像我的代码那样达到目的。

这篇文章解释了为什么没有解决方案。 这篇文章提出了一个使用结构的解决方案,但我想知道是否有更优雅的方法。

#include <functional>
#include <memory>
#include <queue>
#include <random>
#include <utility>
using namespace std;
void f1();
int main() {
srand(0);
f1(); 
return 0;
}
void f1() {
using my_pair = pair<int,int>;
int ref = 2;
function<bool(const my_pair &,const my_pair &)> comp_1 = [&ref](const my_pair &LHS, const my_pair &RHS) {return LHS.first-ref > RHS.first-ref;};
comp_1 = [&ref](const my_pair &LHS, const my_pair &RHS) {return LHS.first < RHS.first;};
// So far so good.
auto comp_2 = [&ref](const my_pair &LHS, const my_pair &RHS) mutable {return LHS.first-ref > RHS.first-ref;};
// Compile error below!
comp_2 = [&ref](const my_pair &LHS, const my_pair &RHS) mutable {return LHS.first < RHS.first;};
// Compile error above
// Applications of the function object
priority_queue<my_pair, vector<my_pair>, decltype(comp_2)> myHeap(comp_2);
for (int i=0; i<10; i++) myHeap.emplace(rand()%10,rand()%20);
while (!myHeap.empty()) {
printf("<%d,%d>n",myHeap.top().first,myHeap.top().second);
myHeap.pop();
}
}

每个 lambda 表达式都是匿名类型的文本;两个不同的 lambda,即使是相同的原型和相同的捕获列表,也是两种不同类型的对象。此外,此类型的赋值运算符被删除,因此您甚至无法将其分配给自身:

$ cat omg.cpp
int main() {
auto f = []{};
f = f;
}
$ g++ omg.cpp
omg.cpp: In function ‘int main()’:
omg.cpp:3:4: error: use of deleted function ‘main()::<lambda()>& main()::<lambda()>::operator=(const main()::<lambda()>&)’
f = f;
^
omg.cpp:2:12: note: a lambda closure type has a deleted copy assignment operator