Do I need to use std::move(...) when returning string result from std::stringstream?
For my implementation of the std-lib the str()
implementation for std::stringstream has a str() &&
version, making it possible to move the string out of the stream without copying it.
My question is, when returning from a function, do you need to specify std::move(...)
or is it assumed automatically and can be left out?
Toy example:
#include <sstream>
#include <iostream>
std::string f() {
auto ss = std::ostringstream{};
ss << std::string(10000000, '*');
// 开发者_JAVA百科return std::move(ss.str()); // Is move() required here?
// return ss.str() // or does this use the str() && implementation?
return std::move(ss).str(); // Syntax suggested by ALX23z
}
int main() {
auto x = f();
std::cout << x << "\n";
}
Edit: Updated to show that my question does not have to do with SSO.
Edit: The answers marked as duplicates does not answer the question I am asking with member functions, and if you were to use the answers given in the "duplicate" questions you would get worse performance. (it's now reopened)
精彩评论