nunojpg
nunojpg

Reputation: 505

C++ move semantics with ostringstream

If a function receives a std::string&&, is there a way to pass a ostringstream without creating a temporary stream with a full duplication of the underlying buffer?

Upvotes: 0

Views: 134

Answers (1)

cdhowie
cdhowie

Reputation: 169281

Ultimately you have to copy the buffer, because it's not in the same structure that std::string requires, and there is no std::string move constructor that accepts std::ostringstream&&. The std::ostringstream::str() method will copy the value of the buffer into an std::string instance. Then you are free to move that string using a method that accepts std::string&&.

Upvotes: 1

Related Questions