Consider the following function which is returning a lambda:
std::function<int()> make_counter()
{
int i = 0;
return [=]() mutable { return i++; };
}
Is it possible to return the actual lambda type, without wrapping it into a std::function
?
C++11: No. Every lambda expression has, I quote (§5.1.2/3):
[...] a unique, unnamed non-union class type [...]
This effectively means that you can't know the lambda's type without knowing the corresponding expression first.
Now, if you didn't capture anything, you could use the conversion to function pointer and return that (a function pointer type), but that's pretty limiting.
As @Luc noted in the Lounge, if you're willing to replace your make_counter
(and if it isn't a template, or overloaded, or something), the following would work:
auto const make_counter = [](int i = 0) {
return [i]() mutable { return i++; };
};
C++1y: Yes, through return-type deduction for normal functions (N3582).