用运算符>>折叠表达式

时间:2018-02-11 09:48:00

标签: c++ variadic-templates fold-expression

请考虑以下代码段:

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

我做错了什么?人们会期望事情完全相同,我只是用运算符<<替换了运算符>>

1 个答案:

答案 0 :(得分:0)

作为@T.C.回答,这是clang中的一个错误。它has been fixed。似乎是一个错字,导致 >>> 在折叠表达式中无法正常工作。

相关问题