3 ms·
In C++23 you can do recursive lambdas by deducing this auto gcd = [](this auto self, int a, int b) -> int { return b == 0 ? a : self(b, a % b);
by RcouF1uZ4gsC 4y ago
In C++23 you can do recursive lambdas by deducing this
auto gcd = [](this auto self, int a, int b) -> int {
return b == 0 ? a : self(b, a % b);
}
std::cout << gcd(20, 30) << std::endl;
- dataangel 4y agowhy isn't it "auto this"? That that would make it <type> <name> like everything else in the language
- kaashif 4y agoThe name is "self" not "this". "this" is a keyword indicating it's the explicit object parameter, it's not even actually a type. C++ seems to be trying to maximise confusion these days.
- jcelerier 4y agostruct foo { void bar(int a, int b) { auto lambda = [&] (auto this self) { this->compute(); }; } void compute(); }; if the variable name was "auto this" you wouldn't be able to refer to "compute" through the "this" of the parent class here