Le lundi 20 juillet 2015 à 12:26 -0700, Thiago Macieira a écrit : > On Monday 20 July 2015 18:25:43 Keith Gardner wrote: > > > What's the difference in std::vector between an rvalue push_back and > > > emplace_back? > > > > emplace_back takes variadic template arguments to construct the item > > directly in the vector instead of creating a temporary and then performing > > a move operation. If the template argument is just an rvalue of the > > container type, it will perform just like the rvalue push_back function. > > > > http://en.cppreference.com/w/cpp/container/vector/emplace_back > > Aside from the variadic and the templateness, what's the difference? > > The templateness changes how a type different than the vector's type gets > constructed (it might undergo a conversion first).
Not sure i understand you well there. The variadic and templateness changes it so that no temporary gets constructed at all, object is constructed in-place with a call to a placement new and T’s constructor with provided arguments. > But assuming I am pushing > back a T, is there any reason I'd want emplace_back? Or vice-versa? emplace_back is really designed to avoid the creation of a temporary object, so, passing a T to it shouldn’t make any difference : that would be in both case a construction via copy (resp move) constructor. regards, Julien Blanc
_______________________________________________ Development mailing list [email protected] http://lists.qt-project.org/mailman/listinfo/development
