Matteo Monti
Matteo Monti

Reputation: 8930

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 >>.

Upvotes: 6

Views: 629

Answers (1)

Yakk - Adam Nevraumont
Yakk - Adam Nevraumont

Reputation: 275210

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.

Upvotes: 1

Related Questions