是否可以找出lambda的参数类型和返回类型? [英] Is it possible to figure out the parameter type and return type of a lambda?
问题描述
给定一个lambda,是否可以找出它的参数类型和返回类型?如果是,如何?
Given a lambda, is it possible to figure out it's parameter type and return type? If yes, how?
基本上,我想要 lambda_traits
,可以通过以下方式使用:
Basically, I want lambda_traits
which can be used in following ways:
auto lambda = [](int i) { return long(i*10); };
lambda_traits<decltype(lambda)>::param_type i; //i should be int
lambda_traits<decltype(lambda)>::return_type l; //l should be long
后面的动机是我想使用 lambda_traits
在接受lambda作为参数的函数模板中,我需要知道它的参数类型和函数中的返回类型:
The motivation behind is that I want to use lambda_traits
in a function template which accepts a lambda as argument, and I need to know it's parameter type and return type inside the function:
template<typename TLambda>
void f(TLambda lambda)
{
typedef typename lambda_traits<TLambda>::param_type P;
typedef typename lambda_traits<TLambda>::return_type R;
std::function<R(P)> fun = lambda; //I want to do this!
//...
}
暂时,假设lambda只需要一个参数。
For the time being, we can assume that the lambda takes exactly one argument.
最初,我尝试使用 std :: function
template<typename T>
A<T> f(std::function<bool(T)> fun)
{
return A<T>(fun);
}
f([](int){return true;}); //error
但它显然会给出错误(ideone )。所以我把它改成了 TLambda
版本的函数模板,并希望在函数中构造 std :: function
(如上所示)。
But it obviously would give error (ideone). So I changed it to TLambda
version of the function template and want to construct the std::function
object inside the function (as shown above).
推荐答案
有趣的是,我刚写了一个 function_traits
com / questions / 2562320 / specializing-a-template-on-a-lambda-in-c0x>在C ++ 0x 中专门用于lambda的模板,它可以提供参数类型。这个问题的答案是使用lambda的 operator()
的 decltype
。
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 <typename T>
struct function_traits
: public function_traits<decltype(&T::operator())>
{};
// For generic types, directly use the result of the signature of its 'operator()'
template <typename ClassType, typename ReturnType, typename... Args>
struct function_traits<ReturnType(ClassType::*)(Args...) const>
// we specialize for pointers to member function
{
enum { arity = sizeof...(Args) };
// arity is the number of arguments.
typedef ReturnType result_type;
template <size_t i>
struct arg
{
typedef typename std::tuple_element<i, std::tuple<Args...>>::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<decltype(lambda)> traits;
static_assert(std::is_same<long, traits::result_type>::value, "err");
static_assert(std::is_same<int, traits::arg<0>::type>::value, "err");
return 0;
}
这篇关于是否可以找出lambda的参数类型和返回类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!