C ++ Lambda返回自身 [英] C++ lambda returning itself
本文介绍了C ++ Lambda返回自身的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想编写一个返回自身的lambda,所以我可以当场多次调用它.但是,如果lambda是在成员函数中定义的,则好像在lambda this
内部不是指lambda,而是指周围对象的this
.
I wanted to write a lambda that returns itself, so I could call it multiple times on the spot. But it looks like inside of a lambda this
refers not to the lambda but to the surrounding object's this
, if the lambda is defines inside a member function.
这是一个例子:
#include <iostream>
int main(int argc, char* argv[]) {
int a = 5;
[&](int b) {
std::cout << (a + b) << std::endl;
return *this;
}(4)(6);
}
有没有办法做可比的事情?
Is there a way to do something comparable?
推荐答案
使用旧函子:
int main() {
int a = 5;
struct S {
const S& operator ()(int b) const {
std::cout << (a + b) << std::endl;
return *this;
}
const int& a;
};
S{a}(4)(6);
}
这篇关于C ++ Lambda返回自身的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文