std::getline( basic_istream<...> &&input, basic_string<...> &str ) with rvalue-"input"

Viewed 85

There's a newer version of std::getline with C++11. It accepts a rvalue "input"-parameter. But why there is a rvalue-parameter, i.e. the fuction would consume the "input"-parameter, leaving the input-parameter empty after the call ?

1 Answers

I imagine it's for code where the stream is constructed as part of the call to getline, e.g

std::string s = ...;
std::string s2;
getline(std::istringstream(s), s2);

This code would take the first line from s and put it in s2, for instance.

Code like this would not be legal with the older version of getline because the first parameter of that is a non-const reference.

Related