I'm trying to change a a tuple of vectors into a vector of tuples (and vice-versa). I'm having trouble calling the tuple_transpose
function. When I call it with one argument I get a no matching function call
error:
prog.cpp: In function ‘int main()’:
prog.cpp:44:24: error: no matching function for call to ‘tuple_transpose(std::tuple >, std::vector > >&)’
prog.cpp:44:24: note: candidates are:
prog.cpp:30:6: note: template typename transpose::type tuple_transpose(std::tuple >...>&, seq)
prog.cpp:30:6: note: template argument deduction/substitution failed:
prog.cpp:44:24: note: candidate expects 2 arguments, 1 provided
prog.cpp:36:6: note: template typename transpose::type tuple_transpose(std::tuple >...>&)
prog.cpp:36:6: note: template argument deduction/substitution failed:
prog.cpp: In substitution of ‘template typename transpose::type tuple_transpose(std::tuple >...>&) [with T = {int, bool}]’:
prog.cpp:44:24: required from here
prog.cpp:36:6: error: no type named ‘type’ in ‘struct transpose >, std::vector > >&>’
#include <vector>
#include <tuple>
#include <type_traits>
template <typename... T>
struct transpose {};
template <typename... T>
struct transpose<std::tuple<std::vector<T>...>>
{
using type = std::vector<std::tuple<T...>>;
};
template <typename... T>
struct transpose<std::vector<std::tuple<T...>>>
{
using type = std::tuple<std::vector<T>...>;
};
// Indicies from Andy Prowl's answer
template <int... Is>
struct seq {};
template <int N, int... Is>
struct gen_seq : gen_seq<N - 1, N - 1, Is...> {};
template <int... Is>
struct gen_seq<0, Is...> : seq<Is...> {};
template <typename... T, int... Is>
auto tuple_transpose(std::tuple<std::vector<T>...>& var, seq<Is...>) -> typename transpose<decltype(var)>::type
{
return { std::make_tuple(std::get<Is>(var)...) };
}
template <typename... T>
auto tuple_transpose(std::tuple<std::vector<T>...>& var) -> typename transpose<decltype(var)>::type
{
return tuple_transpose(var, gen_seq<sizeof...(T)>{});
}
int main()
{
std::tuple<std::vector<int>, std::vector<bool>> var;
tuple_transpose(var); // error
...
}
Here is a demo with the error in it: http://ideone.com/7AWiQQ#view_edit_box
What am I doing wrong and how can I fix it? Thanks.
decltype(var)
is a reference type, but there are other issues with your code, most notably complete absence of conversion logic. I see only unpacking and repacking a tuple. – zch