Consider the following line:
std::string s = (std::stringstream() << "foo").str();
This should not compile because std::stringstream::operator<<()
is inherited by std::ostream
and returns a std::ostream&
which does not have an str()
member.
It seems the main compilers are now accepting this code where they didn't in the past. I would like to understand what standard change happened to make this to compile?
I made some tests with gcc , clang and msvc and I could find the version where the change happened:
Compiler | Rejects until (version) | Accepts from (version) |
---|---|---|
GCC | 11.1 | 11.2 |
Clang | 12.0.1 | 13.0.0 |
MSVC | v19.14 | v19.15 |
You can find the test here
They all added the rvalue overload (see here ) very late.
The rvalue overload was introduced in C++11 and returns the same type of stream as its left-hand operand.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.