- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我有一个在类型参数和参数包上模板化的类,我对这种类型的类型推导感到困惑;在编写输出流操作符时,我在 operator<<
上发现了一个参数包将不匹配模板类的 type 和 pack 参数:
#include <iostream>
template<class T, class... Ts>
struct foo
{ /* ... */ };
template< class... Ts >
std::ostream& operator<<( std::ostream& os, const foo<Ts...>& )
{
return os << 42;
}
int main()
{
std::cout << foo<int>();
}
这在 gcc-4.7.2 和 clang-3.0 上都无法编译,所以我想我在这里误解了规则。
gcc 说(其中第 16 行是输出流调用):
t.cpp:16:28: error: cannot bind ‘std::ostream {aka std::basic_ostream<char>}’ lvalue to ‘std::basic_ostream<char>&&’
In file included from /usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/iostream:40:0,
from t.cpp:1:
/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/ostream:600:5: error: initializing argument 1 of ‘std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&&, const _Tp&) [with _CharT = char; _Traits = std::char_traits<char>; _Tp = foo<int>]’
clang 说:
t.cpp:16:16: error: invalid operands to binary expression ('ostream' (aka 'basic_ostream<char>') and 'foo<int>')
std::cout << foo<int>();
~~~~~~~~~ ^ ~~~~~~~~~~
[--- snip: lots of non-viable candidates from standard library ---]
t.cpp:8:19: note: candidate template ignored: substitution failure [with Ts = <>]
std::ostream& operator<<( std::ostream& os, const foo<Ts...>& )
^
谁能告诉我为什么 operator<<
的参数包不能推论为foo
的类型参数和参数包?
最佳答案
发生的情况是带有模板参数包的模板函数class... Ts
, 参数类型 (P) 为 foo<Ts...>
正在根据 foo<int>
的参数类型 (A) 进行推断.
14.8.2.5/9 这么说:
If P has a form that contains
<T>
or<i>
[it does], then each argument Pi [Ts...
] of the respective template argument list P is compared with the corresponding argument Ai [int
] of the corresponding template argument list of A. If the template argument list of P contains a pack expansion that is not the last template argument, the entire template argument list is a non-deduced context. [the pack expansion is last, so the previous doesnt apply] If Pi is a pack expansion [Ts...
, it is], then the pattern of Pi is compared with each remaining argument in the template argument list of A (int
). Each comparison deduces template arguments for subsequent positions in the template parameter packs expanded by Pi.
所以 class... Ts
应该推导出为一个元素列表int
,因此函数模板应使用参数类型 const foo<int>&
进行实例化,并且是可行的。
这是一个编译器错误。您的代码格式正确。
更简洁地说,这是格式良好的:
template<class A, class... B> struct S { };
template<class... C> void f(S<C...>) { }
int main() { f(S<int>()); }
但至少在 gcc 4.7.2 上同样失败:
error: parameter 1 of ‘void f(S<C ...>) [with C = {int, C}]’
has incomplete type ‘S<int, C>’
C
被错误地推断为 C = {int, C}
(无意义的递归)而不是 C = {int}
. C
的破扣导致更多的垃圾 S<int, C>
类型不完整。
关于c++ - 为什么不会在函数调用中将模板形参包推导出为多个类型实参?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17277487/
我正在尝试创建带有固定三 Angular 形导航的网页。 问题是我无法将较小的三 Angular 形放入大三 Angular 形中,如下图所示。 当调整窗口大小时三 Angular 形正在改变它的 A
我目前正在使用 Angular-material,但我在另一个项目中遇到了一种情况,迫使我使用类似 angular material chips 的东西。效果如本链接所述。 对我来说主要的麻烦是我想在
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 6 年前。 Improve this ques
我通过将一个正方形旋转 45 度创建了一个菱形: .shape { height: 50px; width: 50px; transform: rotate(45deg); } 是否
我使用 css 创建了一个三 Angular 形: .box { width: 0; height: 0; border-style: solid; border-width: 540px 964px
如何创建边框三 Angular 形? 我唯一能想到的就是做一个三 Angular 形 .triangle { width: 0; height: 0;
我想创建一个旋转函数,在该函数中我的三 Angular 形可以像轮子一样自行旋转,但我与移动三 Angular 形的部分代码发生冲突,我尝试了许多解决方案但没有成功,也许如果你们中的一个人知道它会对人
我正在使用线性垫步进器。 它与 next 一起工作正常。我进行 api 调用,如果成功,则调用 stepper-next 事件而不是使用 matStepperNext 指令。 现在,当用户在第 1 步
我想根据用户的 onClick 事件将 V 形从 down 更改为 up。我尝试过使用其他人的其他示例,但没有成功。 这是我的JSFiddle . 最佳答案 嗯,您的 JSFiddle 设置存在一些问
我想在鼠标单击的地方绘制一个 2D 三 Angular 形。已经制作了鼠标事件处理程序,并且可以看到鼠标单击的点。我在缓冲区对象中写入了三 Angular 形的顶点位置。它将是三 Angular 形大
有人可以告诉我我在 Javascript 中的帕斯卡三 Angular 形上做错了什么吗?我看到一个已经存在的使用递归的线程,但是,在没有逐字复制的情况下,在我看来,代码看起来太相似,无法破译我做错了
我必须为我的类(class)使用星号制作一个三 Angular 形和倒三 Angular 形。 我已经制作了上半部分,但是,我在制作上下颠倒的部分时遇到了很大的麻烦 for(var count=1;
我想获取围绕一个点的三 Angular 形的点,其中面指向指定法线的方向。我将使用 THREE.js 将它们添加到 BufferGeometry。 非常粗略的绘图: 这是我到目前为止的代码: //Th
我从编程开始。我正在使用 JavaScript。 为了练习,我打印了一个像这样的三 Angular 形: * ** *** **** ***** 但我想从右向左打印,如下所示: * **
我需要在 Joint JS 中创建一些以圆形源开头并以三 Angular 形结尾的链接,反之亦然,得到了这个,但它不起作用: var link1 = new joint.dia.Link({
这个问题已经有答案了: 奥 git _a (1 个回答) 已关闭 6 年前。 我做了一些安静的搜索,发现了很多将星星和其他形状输出到无数图案中的方法,但我还没有找到任何关于如何使用用户生成的短语来做到
我正在尝试仅使用递归打印出字母 V 的形状。我在这个网站上看到了一些与我的问题相关的代码,但大多数使用循环而不是递归。 这是我的代码: public class Pattern { pub
这个问题在这里已经有了答案: How to Make A Chevron Arrow Using CSS? (10 个答案) 关闭 7 年前。 我想给this triangle中间略有下降,我不想要
我的问题是关于使用 Javascript 对其边进行三 Angular 形评估。以下代码是非常初始的版本,即使它可以工作。我想知道它是否可以更简化,或者有其他方法可以达到相同的结果。 谢谢! let
function makeLine(length) { var line = ""; for (var i = 1; i <= length; i++) { for (var j =
我是一名优秀的程序员,十分优秀!