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.
Here's an example:
#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?
With old functor:
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);
}
Demo
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With