Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++: No matching function call when calling tuple_transpose function

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.

like image 864
Me myself and I Avatar asked Nov 29 '25 15:11

Me myself and I


1 Answers

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.

like image 180
Andy Prowl Avatar answered Dec 02 '25 03:12

Andy Prowl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!