1

Is there a way to get the type of the parameter(s) of a lambda function in C++?

For example one can easily get the return type of a lambda function with decltype():

auto f = [](int x){ return x*5; };
auto lambda_container = create_some_container<decltype(f)>(f);

I want to be able to also get the type of the parameter of the function and to be able to use it in the same manner I use the return type of the lambda function in the example above.

EDIT: Existing answers do not explain how to use the type as a template argument.

yari
  • 165
  • 1
  • 6
  • This is going to be tricky because some lambdas can be considered function pointers, while others you must go through `operator()`. Ultimately we need templates to match "callable" objects and get their parameter lists. Perhaps if we restrict it to callables with 1 argument it gets a little easier. – AndyG Jun 16 '17 at 18:23
  • AFAIK there is no easy way to do this. Once we get reflection, it might be possible. You can certainly get the number of parameters to an arbitrary callable object, but the actual types seems to require something like [Antony Polukhin's magic-get](https://www.youtube.com/watch?v=abdeAew3gmQ) – Justin Jun 16 '17 at 18:23
  • `decltype(f)` doesn't get the return type of the lambda; it gives you the actual type of it. – Justin Jun 16 '17 at 18:24
  • I'm assuming you want this to work for lambdas that might capture a value, not just lambdas which can decay into function pointers. If it's the latter, then this problem is easy. – Justin Jun 16 '17 at 18:26
  • @xaxxon You can only do that if you already know the types... at which point you don't even need to do that? – Barry Jun 16 '17 at 19:07
  • Your code above doesn't get the return type, so "like in the example above" makes no sense. In general, this is impossible, as lambdas exist without fixed parameter types. In specific cases, it is possible, and the duplicate covers that. – Yakk - Adam Nevraumont Jun 16 '17 at 19:46
  • @Yakk There's no "like in the example above" in my post, I'm pretty specific. I really hoped for something as useful as the `decltype()` built-in funciton, but for the types of lambda parameters. – yari Jun 16 '17 at 19:51
  • @yari "[...] same manner I use the return type of the lambda function in the example above."; I was using "like" as a placeholder for the words before "in the example above". In any case, there is no example above that contains the use of the return type. `auto lambda_container = create_some_container(f);` never deduces the return type of `f`. You are not pretty specific, you are extremely vague; possibly you think `decltype(f)` is the return type of `f`? That isn't true, and would explain why the part afterwards doesn't make sense. – Yakk - Adam Nevraumont Jun 16 '17 at 19:54
  • What is the type of the 3rd parameter to `[](auto...xs){return sizeof...(xs);}`? What is the type of the return value of `[](auto x){return x;}`? Lambdas do not in general *have* fixed parameter or return types. – Yakk - Adam Nevraumont Jun 16 '17 at 19:58

0 Answers0