Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fold expressions with operator >>

Consider the following snippet:

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

In writeall, I use fold expressions to feed into std :: cout a parameter pack. Everything works perfectly, and I get 1234 printed out to screen.

In readall, I do exactly the same, expecting to read from std :: cin a parameter pack. However, I get

error: expected ')'
(std :: cin >> ... >> items);

What am I doing wrong? One would expect things to work exactly the same, I just replaced operator << with operator >>.

like image 627
Matteo Monti Avatar asked Nov 18 '22 20:11

Matteo Monti


1 Answers

As @T.C. answered, this is a bug in clang. It has been fixed. There was what appeared to be a typo that made > and >> not work right in fold expressions.

like image 161
Yakk - Adam Nevraumont Avatar answered Dec 05 '22 18:12

Yakk - Adam Nevraumont