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
The specialization method shown in @KennyTMs answer can be extended to cover all cases, including variadic and mutable lambdas:
template
struct closure_traits : closure_traits {};
#define REM_CTOR(...) __VA_ARGS__
#define SPEC(cv, var, is_var) \
template \
struct closure_traits \
{ \
using arity = std::integral_constant; \
using is_variadic = std::integral_constant; \
using is_const = std::is_const; \
\
using result_type = R; \
\
template \
using arg = typename std::tuple_element>::type; \
};
SPEC(const, (,...), 1)
SPEC(const, (), 0)
SPEC(, (,...), 1)
SPEC(, (), 0)
Demo.
Note that the arity is not adjusted for variadic operator()
s. Instead one can also consider is_variadic
.