Is it possible to figure out the parameter type and return type of a lambda?

后端 未结 4 1821
[愿得一人]
[愿得一人] 2020-11-22 02:08

Given a lambda, is it possible to figure out it\'s parameter type and return type? If yes, how?

Basically, I want lambda_traits which can be used in fol

4条回答
  •  后悔当初
    2020-11-22 02:33

    The answer provided by @KennyTMs works great, however if a lambda has no parameters, using the index arg<0> does not compile. If anyone else was having this problem, I have a simple solution (simpler than using SFINAE related solutions, that is).

    Just add void to the end of the tuple in the arg struct after the variadic argument types. i.e.

    template 
        struct arg
        {
            typedef typename std::tuple_element>::type type;
        };
    

    since the arity isn't dependent on the actual number of template parameters, the actual won't be incorrect, and if it's 0 then at least arg<0> will still exist and you can do with it what you will. If you already plan to not exceed the index arg then it shouldn't interfere with your current implementation.

提交回复
热议问题