我用来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 中都会出现模板推导问题:
叮当输出:
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.
海合会输出:
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()
扁平递归尾元组,添加一个头,然后返回元组。转发用于在递归期间不丢弃引用限定符。
稍后在代码中,结果元组被解包以调用不同的可变参数函数,但这超出了此错误的范围。
显然,我在某处省略了一些微妙但至关重要的细节,但我发现在追查潜在问题方面存在很大困难。