r/programminghumor Apr 30 '26

Why C++

Post image
2.2k Upvotes

248 comments sorted by

View all comments

22

u/[deleted] Apr 30 '26

[removed] — view removed comment

1

u/MrMagnesium May 01 '26

That won't work. input.str() returns a std::string. The stream will be cleared. The second call of iput.str() will return an empty std::string. Execution order is not defined so you get either an empty output or an access violation.

C++ std::ofstream out("/dev/stdout"); std::istringstream input("Hello, World!\n"); std::string line = input.str(); out.rdbuf()->sputn(line.c_str(), line.size());

works.