使用运算符>>折叠表达式

Fold expressions with operator >>

本文关键字:gt 表达式 折叠 运算符      更新时间:2023-10-16

考虑以下片段:

#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);

我在做什么错?人们期望事情可以完全相同,我刚刚用操作员>>替换了操作员<<

as @t.c。回答说,这是Clang中的一个错误。它已经固定了。似乎有一个错字使>>>在折叠表达式中不正常。