std::vector::emplace_back and std::move

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.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)