Search code examples
c++c++14generic-lambda

Arity of a generic lambda


It is possible to deduce arity of a non-generic lambda by accessing its operator().

template <typename F>
struct fInfo : fInfo<decltype(&F::operator())> { };

template <typename F, typename Ret, typename... Args>
struct fInfo<Ret(F::*)(Args...)const> { static const int arity = sizeof...(Args); };

This is nice and dandy for something like [](int x){ return x; } as the operator() is not templated.

However, generic lambdas do template the operator() and it is only possible to access a concrete instantiation of the template - which is slightly problematic because I can't manually provide template arguments for the operator() as I don't know what its arity is.

So, of course, something like

auto lambda = [](auto x){ return x; };
auto arity = fInfo<decltype(lambda)>::arity;

doesn't work.

I don't know what to cast to nor do I know what template arguments to provide (or how many) (operator()<??>).
Any ideas how to do this?


Solution

  • It's impossible, as the function call operator can be a variadic template. It's been impossible to do this forever for function objects in general, and special-casing lambdas because they happened to not be equally powerful was always going to be a bad idea. Now it's just time for that bad idea to come home to roost.