3
votes

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.

1
What do you mean "change a a tuple of vectors into a vector of tuples". Transpose? What if vectors have different lengths? The current error is because 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
Your idea with expanding the vector in a braced-init-list cannot work as the size of the vector is only known at run-time. (See Andy Prowl's answer.)dyp

1 Answers

3
votes

If you are assuming vectors of identical sizes, this should do the job:

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 transpose(std::tuple<std::vector<T>...>& var, seq<Is...>)
    -> std::vector<std::tuple<T...>>
{
    std::vector<std::tuple<T...>> result;
    for (std::size_t i = 0; i < std::get<0>(var).size(); i++)
    {
        std::tuple<T...> t = std::make_tuple(std::get<Is>(var)[i]...);
        result.push_back(t);
    }

    return result;
}

template <typename... T, int... Is>
auto transpose(std::tuple<std::vector<T>...>& var)
    -> std::vector<std::tuple<T...>>
{
    return transpose(var, gen_seq<sizeof...(T)>());
}

And here is how you could test it:

#include <iostream>
#include <iomanip>

int main()
{
    std::vector<int> vi = {42, 1729, 6};
    std::vector<bool> vb = {true, false, false};
    std::vector<std::string> vs = {"Hi", "Hey", "Ho"};

    auto t = make_tuple(vi, vb, vs);
    auto v = transpose(t);

    std::cout << std::boolalpha;
    for (auto const& t : v)
    {
        std::cout << "(";
        std::cout << std::get<0>(t);
        std::cout << ", " << std::get<1>(t);
        std::cout << ", " << std::get<2>(t);
        std::cout << ")" << std::endl;
    }
}

Finally, a live example.