- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我使用 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/
我有从其他来源获取的代码。其余代码效果很好。我正在尝试使用以下代码附加到元组: // Because std::make_tuple can't be passed // to higher orde
我使用 std::tuple_cat 将参数列表的子集选择转换为元组,如下所示: template struct odds; template struct odds { typedef
我需要一个与 std::tuple_cat 非常相似的 constexpr 函数,但不是将所有元素(无论它们是什么)合并到一个元组中,仅当尚未添加该类型时,我才需要它来添加给定元素。 将谓词传递给 s
基础问题 我要解决的基本问题是: 我有一个模板参数包 ArgTypes,我需要用包装在 std::optional 中的每个类型创建一个元组。例如:make_optional_tuple应该返回 st
我想从我的函数返回 std::tuple_cat 的结果,但我无法推断出返回类型 #include struct H { typedef std::tuple tuple_type;
我最近将 C++ 代码从 VS2012 迁移到 VS2013。代码在 VS2012 下编译,但 VS2013 抛出 C1001 内部编译器错误。 具体来说,错误指向std库中的tuple.h文件: t
std::experimental::apply具有以下签名: template constexpr decltype(auto) apply(F&& f, Tuple&& t); 它基本上通过扩展
如果我对 std::tuple_cat 进行完整、合格的调用,则以下代码将使用 MSVC、GCC 和 Clang 进行编译.但是如果我对 tuple_cat 进行不合格的调用,它不会在这些编译器中的任
我是一名优秀的程序员,十分优秀!