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
Funny, I've just written a function_traits implementation based on Specializing a template on a lambda in C++0x which can give the parameter types. The trick, as described in the answer in that question, is to use the decltype
of the lambda's operator()
.
template
struct function_traits
: public function_traits
{};
// For generic types, directly use the result of the signature of its 'operator()'
template
struct function_traits
// we specialize for pointers to member function
{
enum { arity = sizeof...(Args) };
// arity is the number of arguments.
typedef ReturnType result_type;
template
struct arg
{
typedef typename std::tuple_element>::type type;
// the i-th argument is equivalent to the i-th tuple element of a tuple
// composed of those arguments.
};
};
// test code below:
int main()
{
auto lambda = [](int i) { return long(i*10); };
typedef function_traits traits;
static_assert(std::is_same::value, "err");
static_assert(std::is_same::type>::value, "err");
return 0;
}
Note that this solution does not work for generic lambda like [](auto x) {}
.