[英]Extracting function argument types as a parameter pack
This is a followup question to "unpacking" a tuple to call a matching function pointer , which asked how to provide the values from a std::tuple
as arguments to a function in a generic way. 这是“解包”元组以调用匹配的函数指针的后续问题,该指针询问如何以通用方式将
std::tuple
的值提供为函数的参数。 A solution given there was the following: 给出的解决方案如下:
template<int ...>
struct seq { };
template<int N, int ...S>
struct gens : gens<N-1, N-1, S...> { };
template<int ...S>
struct gens<0, S...>
{
typedef seq<S...> type;
};
double foo(int x, float y, double z)
{
return x + y + z;
}
template <typename... Args>
struct save_it_for_later
{
std::tuple<Args...> params;
double (*func)(Args...);
double delayed_dispatch()
{
return callFunc(typename gens<sizeof...(Args)>::type());
}
template<int ...S>
double callFunc(seq<S...>)
{
return func(std::get<S>(params) ...);
}
};
int main(void)
{
std::tuple<int, float, double> t = std::make_tuple(1, 1.2, 5);
save_it_for_later<int,float, double> saved = {t, foo};
std::cout << saved.delayed_dispatch() << std::endl;
}
My question is whether there's way to make an alternate version of save_it_for_later
which takes only foo
as a template argument, so that we don't have to provide foo
's parameter types as a template argument (or bake its return type into save_it_for_later
). 我的问题是是否有办法制作仅使用
foo
作为模板参数的save_it_for_later
的替代版本,这样我们就不必提供foo
的参数类型作为模板参数(或将其返回类型烘烤到save_it_for_later
)。 Something like 就像是
int main(void) {
...
save_it_for_later2<foo> saved = {t};
...
}
I'd be equally fine with some sort of macro wrapping foo
to extract the required types: 我也可以使用某种宏包装
foo
来提取所需的类型:
int main(void) {
...
save_it_for_later<MACRO_USING_DECLTYPE_OR_SOMESUCH(foo)> saved = {t};
...
}
This concern seems orthogonal enough to the original question to warrant its own ticket. 这种担忧似乎与最初的问题正交,足以保证其本身的效力。
#include <tuple>
#include <utility>
template <typename> struct save_it_for_later_t;
template <typename Result, typename... Args>
struct save_it_for_later_t<Result (*)(Args...)> {
std::tuple<Args...> params;
Result (*fun)(Args...);
template <typename... Params>
save_it_for_later_t(Result (*fun)(Args...), Params&&... params)
: params(std::forward<Params>(params)...)
, fun(fun) {
}
// ...
};
template <typename Result, typename... Args, typename... Params>
save_it_for_later_t<Result(*)(Args...)>
save_it_for_later(Result (*fun)(Args...), Params&&... params) {
return save_it_for_later_t<Result(*)(Args...)>(fun, std::forward<Params>(params)...);
}
double foo(float, float, double);
int main() {
auto saved = save_it_for_later(foo, 1.2f, 3.4f, 5.6);
// ...
}
I just sheepishly discovered that I'd asked a similar question last year ( Unpacking arguments of a functional parameter to a C++ template class ), which yields an answer here too: 我只是令人毛骨悚然地发现,去年我问了一个类似的问题( 将功能参数的参数解压缩到C ++模板类中 ),这在这里也产生了答案:
#include <functional>
#include <iostream>
#include <tuple>
template<int ...>
struct seq { };
template<int N, int ...S>
struct gens : gens<N-1, N-1, S...> { };
template<int ...S>
struct gens<0, S...>
{
typedef seq<S...> type;
};
double foo(int x, float y, double z)
{
return x + y + z;
}
template<typename T>
struct save_it_for_later;
template <typename Result, typename... Args>
struct save_it_for_later<Result(Args...)>
{
std::tuple<Args...> params;
Result (*func)(Args...);
Result delayed_dispatch()
{
return callFunc(typename gens<sizeof...(Args)>::type());
}
template<int ...S>
Result callFunc(seq<S...>)
{
return func(std::get<S>(params) ...);
}
};
int main(void)
{
std::tuple<int, float, double> t = std::make_tuple(1, 1.2, 5);
save_it_for_later<decltype(foo)> saved = {t, foo};
std::cout << saved.delayed_dispatch() << std::endl;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.