我使用 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()
扁平化递归尾部元组、添加头部并返回元组。使用转发是为了在递归期间不丢弃引用限定符。
稍后在代码中,生成的元组被解包以调用不同的可变参数函数,但这超出了此错误的范围。
显然,我在某处遗漏了一些细微但重要的细节,但我发现要追查根本问题非常困难。
最佳答案
tuple_cat
的参数必须是元组(或支持 std::tuple_size
和 std::tuple_element
API 的“类元组”事物,例如 std::pair
或 std::array
)但从错误消息来看,您似乎是在使用非元组类型调用它。为此,您可能需要类似的东西:
std::tuple_cat(std::make_tuple(a_non_tuple), a_tuple, another_tuple);
这会将第一个参数变成 tuple<decltype(a_non_tuple)>
所以它可以与其他元组连接。
to concatenate
std::tuple<T1, std::tuple<T2, std::tuple<T3, ...>>>
intostd::tuple<T1, T2, T3, ...>
那不是“串联”。我认为正确的说法是“扁平化”。
我不太确定你在尝试什么,因为你没有提供完整的例子。你在打电话吗 foo
像 std::tuple<T1, std::tuple<T2, std::tuple<T3, ...>>>
这样的论点?
那行不通,因为 foo
将推导出T
作为std::tuple<T1, std::tuple<T2, std::tuple<T3, ...>>>
并实例化 odds<T1>
专门化,它只是将参数包装到 foo
在另一个元组中!
关于c++ - std::tuple_cat 替换失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13892194/