Is there any advantage of using std::vector::emplace_back
and std::move
together? or it is just redundant since std::vector::emplace_back
will do an inplace-construction?
Cases for clarification:
std::vector<std::string> bar;
First:
bar.emplace_back(std::move(std::string("some_string")));
Second:
std::string str("some_string");
bar.emplace_back(std::move(str));
Third:
bar.emplace_back(std::move("some_string"));
In the second version, there is an advantage. Calling emplace_back
will call the move constructor of std::string
when std::move
is used, which could save on a copy (so long as that string isn't stored in a SSO buffer). Note that this is essentially the same as push_back
in this case.
std::move
in the first version is unnecessary, as the string is already a prvalue.
std::move
in the third version is irrelevant, as a string literal cannot be moved from.
The simplest and most efficient method is this:
bar.emplace_back("some_string");
That requires no unnecessary std::string
constructions as the literal is perfect-forwarded to the constructor.