我試圖將矢量元組更改爲元組矢量(反之亦然)。在撥打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
什麼我做錯了,如何能我修復它?謝謝。
你是什麼意思「將一個向量元組變成一個元組向量」。移調?如果媒介有不同的長度呢?當前的錯誤是因爲'decltype(var)'是一個引用類型,但是代碼還有其他問題,最顯着的是完全沒有轉換邏輯。我只看到拆包和重新包裝一個元組。 – zch
在擴展init-list中的向量時,您的想法無法正常工作,因爲只有在運行時才知道向量的大小。 (請參閱Andy Prowl的回答。) – dyp