【发布时间】:2018-02-11 09:47:59
【问题描述】:
考虑以下 sn-p:
#include <iostream>
template <typename... types> void writeall(const types & ... items)
{
(std :: cout << ... << items);
}
template <typename... types> void readall(types & ... items)
{
(std :: cin >> ... >> items);
}
int main()
{
writeall(1, 2, 3, 4);
std :: cout << std :: endl;
int a, b, c, d;
readall(a, b, c, d);
}
在writeall 中,我使用折叠表达式输入std :: cout 参数包。一切正常,我将1234 打印到屏幕上。
在readall 中,我做的完全一样,期望从std :: cin 读取参数包。但是,我得到了
error: expected ')'
(std :: cin >> ... >> items);
我做错了什么?人们会期望事情完全一样,我只是将操作员<< 替换为操作员>>。
【问题讨论】:
-
我认为
(std :: cout << ... << items);和(std :: cin >> ... >> items);这两个表达式的行为应该或多或少相同......我错了吗?我错过了什么? -
呃。我知道了。可恶的。是否已在任何较新版本的 Clang 中修复此问题?我正在使用 5.0.1。
-
@MatteoMonti 在 Clang HEAD 7.0 中修复 wandbox.org/permlink/aedqC0BdJbYa3lJX
-
用GCC 7.3编译
标签: c++ variadic-templates fold-expression