2013-05-15 94 views
2

我試圖將矢量元組更改爲元組矢量(反之亦然)。在撥打tuple_transpose功能時遇到問題。當我用一個參數調用它,我得到一個沒有匹配的函數調用錯誤:沒有匹配的函數調用錯誤C++

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

這裏是它與錯誤演示 - http://ideone.com/7AWiQQ#view_edit_box

什麼我做錯了,如何能我修復它?謝謝。

+0

你是什麼意思「將一個向量元組變成一個元組向量」。移調?如果媒介有不同的長度呢?當前的錯誤是因爲'decltype(var)'是一個引用類型,但是代碼還有其他問題,最顯着的是完全沒有轉換邏輯。我只看到拆包和重新包裝一個元組。 – zch

+0

在擴展init-list中的向量時,您的想法無法正常工作,因爲只有在運行時才知道向量的大小。 (請參閱Andy Prowl的回答。) – dyp

回答

2

如果我們假定你是大小相同的載體,這應該做的工作:

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

相關問題