[英]How to extract lambda's Return Type and Variadic Parameters Pack back from general template<typename T>
I want to create a templated class or function, that receives a lambda, and puts it internally in std::function<> Lambda could have any number of input parameters [](int a, float b, ...) std::function<> should correspond to the lambda's operator()'s type 我想创建一个模板化的类或函数,它接收一个lambda,并将它放在std :: function <>内部.Lambda可以有任意数量的输入参数[](int a,float b,...)std :: function <>应该对应lambda的operator()的类型
template <typename T>
void getLambda(T t) {
// typedef lambda_traits::ret_type RetType; ??
// typedef lambda_traits::param_tuple --> somehow back to parameter pack Args...
std::function<RetType(Args...)> fun(t);
}
int main() {
int x = 0;
getLambda([&x](int a, float b, Person c){});
}
So I need to somehow extract the Return Type and Parameter Pack 所以我需要以某种方式提取返回类型和参数包
Answer here suggests to use partial spec on lambda's :: operator() 这里的答案建议在lambda的:: operator()上使用部分规范
template <typename T>
struct function_traits : public function_traits<decltype(&T::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.
};
};
But I need a way to convert tuple<> back to parameters pack, to create a proper std::function<> instantiation 但我需要一种方法将元组<>转换回参数包,以创建一个正确的std :: function <>实例化
template <typename T>
struct function_traits : public function_traits<decltype(&T::operator())>
{};
template <typename ClassType, typename ReturnType, typename... Args>
struct function_traits<ReturnType(ClassType::*)(Args...) const>
// we specialize for pointers to member function
{
using result_type = ReturnType;
using arg_tuple = std::tuple<Args...>;
static constexpr auto arity = sizeof...(Args);
};
template <class F, std::size_t ... Is, class T>
auto lambda_to_func_impl(F f, std::index_sequence<Is...>, T) {
return std::function<typename T::result_type(std::tuple_element_t<Is, typename T::arg_tuple>...)>(f);
}
template <class F>
auto lambda_to_func(F f) {
using traits = function_traits<F>;
return lambda_to_func_impl(f, std::make_index_sequence<traits::arity>{}, traits{});
}
The code above should do what you want. 上面的代码应该做你想要的。 The main idea, as you can see, is to create an integer pack. 如您所见,主要思想是创建一个整数包。 This is the non-type template equivalent of variadics. 这是variadics的非类型模板。 I don't know of any technique by which you can use such a pack without calling another function, so typically in these situations with tuples you'll see a nested "impl" function that does all the work. 我不知道你可以使用这种方法而不调用另一个函数的任何技术,所以通常在这些带有元组的情况下你会看到一个嵌套的“impl”函数来完成所有的工作。 Once you have the integer pack, you expand it while accessing the tuple (works for getting the values too). 获得整数包之后,在访问元组时展开它(用于获取值)。
On a stylistic note: use using
, not typename
, especially in template heavy code as the former can alias templates too. 在风格上注意:使用using
而不是typename
, 尤其是在模板繁重的代码中,因为前者也可以使用别名模板。 And don't use that enum
trick to store a static value without it using space; 并且不使用该enum
技巧来存储静态值而不使用空间; compilers will optimize this out anyhow and just using a static constexpr
integer is much clearer. 编译器无论如何都会优化它,只需使用static constexpr
整数static constexpr
清楚了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.