考虑以下代码片段:
#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);
我做错了什么?人们会期望事情的工作方式完全相同,我只是将运算符
<<
替换为运算符 >>
。