int main() {
auto a = []{ return 5; };
auto b = []{ +a; a(); }; // Error, “a” not captured.
}
A stateless lambda object is empty, so it’s unfortunate that they must be captured for use by another lambda. In this case,
b cannot be stateless.
C++17 constexpr lambdas offer a partial workaround:
int main() {
constexpr auto a = +[]{ return 5; };
auto b = []{ +a; a(); }; // OK, “a” is a constant id-expression used with L-to-R conversion.
}
This is a bit esoteric, and it can possibly cost some performance.
Perhaps it should be permissible to call, convert, and even copy a stateless lambda from an enclosing scope. (Binding to a reference or taking an address are genuinely problematic, though. Pass-by-reference would be broken, but fortunately <algorithm> and such pass by value.)