C ++:调用tuple_transpose函数时没有匹配的函数调用

时间:2013-05-15 22:54:37

标签: c++ templates c++11 tuples

我正在尝试将一个元组的元组更改为元组的向量(反之亦然)。我在调用tuple_transpose函数时遇到问题。当我用一个参数调用它时,我得到一个no matching function call错误:

  

prog.cpp:在函数'int main()'中:
  prog.cpp:44:24:错误:没有用于调用'tuple_transpose(std :: tuple>,std :: vector>>&)'的匹配函数   prog.cpp:44:24:注意:候选人是:
  prog.cpp:30:6:注意:模板类型名称transpose :: type tuple_transpose(std :: tuple> ...>&,seq)
  prog.cpp:30:6:注意:模板参数扣除/替换失败:
  prog.cpp:44:24:注意:候选人需要2个参数,1个提供
  prog.cpp:36:6:注意:模板类型名称transpose :: type tuple_transpose(std :: tuple> ...>&)
  prog.cpp:36:6:注意:模板参数扣除/替换失败:
  prog.cpp:替换'template typename transpose :: type tuple_transpose(std :: tuple> ...>&)[with T = {int,bool}]':
  prog.cpp:44:24:从这里要求
  prog.cpp:36:6:错误:'struct transpose>,std :: vector>中没有名为'type'的类型>&安培;>”

#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
    ...
}

以下是包含错误的演示:http://ideone.com/7AWiQQ#view_edit_box

我做错了什么,我该如何解决?感谢。

1 个答案:

答案 0 :(得分:3)

如果你假设相同大小的向量,这应该做的工作:

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)>());
}

以下是您可以测试它的方法:

#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;
    }
}

最后,live example