我用std::tuple_cat
做參數列表的子集選擇成元組,像這樣:的std :: tuple_cat替換故障
template <class...>
struct odds;
template <class T1>
struct odds<T1>
{
typedef std::tuple<T1> type;
static type value(T1&& t1)
{
return std::make_tuple(std::forward<T1>(t1));
}
};
template <class T1, class T2>
struct odds<T1, T2>
{
typedef std::tuple<T1> type;
static type value(T1&& t1, T2&&)
{
return std::make_tuple(std::forward<T1>(t1));
}
};
template <class T1, class T2, class... TTail>
struct odds<T1, T2, TTail...>
{
typedef decltype(std::tuple_cat(T1(), typename odds<TTail...>::type())) type; // L32
static type value(T1&& t1, T2&&, TTail&&... rest)
{
return std::tuple_cat(std::forward<T1>(t1), odds<TTail...>::value(std::forward<TTail>(rest)...)); // L35
}
};
,具有以下的測試案例:
// assume <tuple>, <utility> are included at top of file
template <class... T>
auto foo(T... x) -> typename odds<T...>::type
{
return odds<T...>::value(x...);
//...
}
int main() {
auto bar = foo(5, true, 6, false); // L46
auto baz = odds<int, bool, int, bool>::value(5, true, 6, false); // L47
// bar, baz should be tuple<int,int> with value { 5, 6 }
}
然而,模板扣除問題出現在clang-3.1和gcc-4.7.2中:
Clang輸出:
test.cc:32:19: error: no matching function for call to 'tuple_cat'
typedef decltype(std::tuple_cat(T1(), typename odds<TTail...>::type())) type;
^~~~~~~~~~~~~~
test.cc:40:30: note: in instantiation of template class 'odds<int, bool, int, bool>' requested here
auto foo(T... x) -> typename odds<T...>::type
^
test.cc:40:6: note: while substituting deduced template arguments into function template 'foo' [with T = <int, bool, int, bool>]
auto foo(T... x) -> typename odds<T...>::type
^
/usr/include/c++/v1/tuple:1063:1: note: candidate template ignored: substitution failure [with _Tuple0 = int, _Tuples = <std::__1::tuple<int>>]
tuple_cat(_Tuple0&& __t0, _Tuples&&... __tpls)
^
/usr/include/c++/v1/tuple:987:1: note: candidate function not viable: requires 0 arguments, but 2 were provided
tuple_cat()
^
test.cc:46:13: error: no matching function for call to 'foo'
auto bar = foo(5, true, 6, false);
^~~
test.cc:40:6: note: candidate template ignored: substitution failure [with T = <int, bool, int, bool>]
auto foo(T... x) -> typename odds<T...>::type
^
test.cc:35:10: error: no matching function for call to 'tuple_cat'
return std::tuple_cat(std::forward<T1>(t1), odds<TTail...>::value(std::forward<TTail>(rest)...));
^~~~~~~~~~~~~~
test.cc:47:41: note: in instantiation of member function 'odds<int, bool, int, bool>::value' requested here
auto baz = odds<int, bool, int, bool>::value(5,true,6,false);
^
/usr/include/c++/v1/tuple:1063:1: note: candidate template ignored: substitution failure [with _Tuple0 = int, _Tuples = <std::__1::tuple<int>>]
tuple_cat(_Tuple0&& __t0, _Tuples&&... __tpls)
^
/usr/include/c++/v1/tuple:987:1: note: candidate function not viable: requires 0 arguments, but 2 were provided
tuple_cat()
^
3 errors generated.
gcc的輸出:
test.cc: In instantiation of ‘struct odds<int, bool, int, bool>’:
test.cc:40:6: required by substitution of ‘template<class ... T> typename odds<T ...>::type foo(T ...) [with T = {int, bool, int, bool}]’
test.cc:46:34: required from here
test.cc:32:74: error: no matching function for call to ‘tuple_cat(int, odds<int, bool>::type)’
test.cc:32:74: note: candidate is:
In file included from test.cc:1:0:
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.2/include/g++-v4/tuple:1027:5: note: template<class ... _Tpls, class> constexpr typename std::__tuple_cat_result<_Tpls ...>::__type std::tuple_cat(_Tpls&& ...)
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.2/include/g++-v4/tuple:1027:5: note: template argument deduction/substitution failed:
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.2/include/g++-v4/tuple:1024:31: error: no type named ‘type’ in ‘struct std::enable_if<false, void>’
test.cc: In function ‘int main()’:
test.cc:46:34: error: no matching function for call to ‘foo(int, bool, int, bool)’
test.cc:46:34: note: candidate is:
test.cc:40:6: note: template<class ... T> typename odds<T ...>::type foo(T ...)
test.cc:40:6: note: substitution of deduced template arguments resulted in errors seen above
test.cc:46:34: error: unable to deduce ‘auto’ from ‘<expression error>’
test.cc:47:13: error: ‘value’ is not a member of ‘odds<int, bool, int, bool>’
test.cc:47:61: error: unable to deduce ‘auto’ from ‘<expression error>’
gcc的是多一點有益的這裏,特別是與錯誤
test.cc:32:74: error: no matching function for call to ‘tuple_cat(int, odds<int, bool>::type)’
的目標是要調用的遞歸解壓縮參數的函數,收集選擇成收集元組,並返回它。爲了以平坦的方式積累它,我使用std::tuple_cat()
來平滑遞歸尾元組,添加一個頭並返回元組。轉發用於在遞歸期間不丟棄引用限定符。
在代碼的後面,解壓縮結果元組以調用不同的可變參數函數,但這超出了此錯誤的範圍。很顯然,我在某處遺漏了一些微妙但至關重要的細節,但我發現在追查潛在問題方面存在很大困難。
請提供,實際上可以被編譯到重現你的錯誤一個完整的例子。 –