我正在尝试存储std::tuple不同数量的值,这些值稍后将用作调用与存储类型匹配的函数指针的参数.
我创建了一个简化的示例,显示了我正在努力解决的问题:
#include <iostream>
#include <tuple>
void f(int a, double b, void* c) {
std::cout << a << ":" << b << ":" << c << std::endl;
}
template <typename ...Args>
struct save_it_for_later {
std::tuple<Args...> params;
void (*func)(Args...);
void delayed_dispatch() {
// How can I "unpack" params to call func?
func(std::get<0>(params), std::get<1>(params), std::get<2>(params));
// But I *really* don't want to write 20 versions of dispatch so I'd rather
// write something like:
func(params...); // Not legal
}
}; …Run Code Online (Sandbox Code Playgroud) c++ function-pointers variadic-templates c++11 iterable-unpacking
我两个月前发现了boost :: hana.看起来非常强大所以我决定看一看.从文档中我看到了这个例子:
std::string s;
hana::int_c<10>.times([&]{ s += "x"; });
Run Code Online (Sandbox Code Playgroud)
这相当于:
s += "x"; s += "x"; ... s += "x"; // 10 times
Run Code Online (Sandbox Code Playgroud)
我想知道是否有可能(如果是的话)写smthg:
std::string s;
std::array<int, 10> xs = {1, 3, 5, ...};
hana::int_c<10>.times([&](int i){ s += std::to_string(xs[i]) + ","; });
Run Code Online (Sandbox Code Playgroud)
在编译时,甚至是一种"解包":
myfunction( hana::unpack<...>( xs ) );
Run Code Online (Sandbox Code Playgroud)