I think that using emplace(c.end(),_1) would be same as emplace_back(_1)
But I am not understanding why the language designers gave two functions instead of one.
I am assuming I am missing some information.
So what is it that differentiate emplace(c.end(),_1) from emplace_back(_1) ?
The big difference is the requirements on the container’s
value_typeis lessened forvector::emplace_backanddeque::emplace_backcompared toemplace.Speaking for
vectoranddequeonly:In addition to
EmplaceConstructiblefromargs;emplacerequiresMoveInsertableandMoveAssignable, whereasemplace_backonly requiresMoveInsertable.For most allocators, including
std::allocator,MoveInsertableis the same asMoveConstructible.So if you have a type that you want to put into a
vectorordeque, and it isMoveConstructible, but notMoveAssignable,emplace_backis your friend. It is also likely to be slightly faster, and have slightly smaller code size, but that is a quality-of-implementation issue (not guaranteed by the standard). And the difference is likely to be smaller than you will notice (unless measuring carefully).For example, given:
This compiles:
But this does not:
However if I changed:
to:
then both examples would compile (and run fine).