emplace_back不应该接受一个类型的参数vector::value_type,而应该将可变参数转发给附加项目的构造函数。
template <class... Args> void emplace_back(Args&&... args);
有可能传递一个value_type将被转发给复制构造函数的东西。
因为它转发的参数,这意味着如果你没有右值,这仍然意味着容器将存储“复制”的副本,而不是一个移动的副本。
std::vector<std::string> vec;
vec.emplace_back(std::string("Hello")); // moves
std::string s;
vec.emplace_back(s); //copies
但是,以上应该是相同的push_back。这可能是用于像这样的用例:
std::vector<std::pair<std::string, std::string> > vec;
vec.emplace_back(std::string("Hello"), std::string("world"));
// should end up invoking this constructor:
//template<class U, class V> pair(U&& x, V&& y);
//without making any copies of the strings... 展开详请