returning a lambda without 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).


If you cheat and use return type deduction, yes you can (Link).

Note this is only possible beyond C++11 itself, though it can be accomplished in regular, non-warning-inducing C++11 using lambdas (that is, a lambda inside of a lambda that returns that lamba).