emplace_back
calls to somehthing like
new (data+size) T(std::forward<Args>(args)...);
if args
are basic - non - rvalue-referenced std::string
, the expression will compile to
new (data+size) std::string(str); //str is lvalue - calls std::string::string(const string& rhs)
meaning the copy constructor will take place.
but, if you use std::move
on str
, the code will compile to
new (data+size) std::string(str); //str is r-value reference, calls std::string::string(string&& rhs)
so move semantics takes place. this is a huge performance gain.
do note, that str
is lvalue, it has a name, so in order to create r-value-reference from it, you must use std::move
.
in the example
vec.emplace_back("some literal");
the code will compile to
new (data+size) std::string("literal"); //calls std::string::string(const char*);
so no temporaries.
the third example is nonsense. you cannot move literals.