#include <tuple>
#include <vector>
#include <iostream>
using namespace std;
template<size_t... I>
auto v2t(const auto& v, index_sequence<I...>)
{
return make_tuple(v[I]...);
}
template<size_t N, class T>
auto makeTupleFromVector(const vector<T>& v)
{
return v2t(v, make_index_sequence<N>());
}
void f1(int a1, int a2, int a3)
{
cout<<a1<<" "<<a2<<" "<<a3<<endl;
}
void f2(const tuple<int, int, int>& t)
{
auto [a1, a2, a3] = t;
cout<<a1<<" "<<a2<<" "<<a3<<endl;
}
int main()
{
vector<int> d{1, 2, 3};
auto t = makeTupleFromVector<3>(d);
apply(f1, t);
f2(t);
return 0;
}
运行程序输出:
1 2 3
1 2 3可以看到vector确实被转换为了tuple,并可以通过apply完成调用