The big difference is the requirements on the container's value_type
is lessened for vector::emplace_back
and deque::emplace_back
compared to emplace
.
Speaking for vector
and deque
only:
In addition to EmplaceConstructible
from args
;
emplace
requires MoveInsertable
and MoveAssignable
, whereas
emplace_back
only requires MoveInsertable
.
For most allocators, including std::allocator
, MoveInsertable
is the same as MoveConstructible
.
So if you have a type that you want to put into a vector
or deque
, and it is MoveConstructible
, but not MoveAssignable
, emplace_back
is 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:
#include <vector>
struct A
{
A() = default;
A(A&&) = default;
A& operator=(A&&) = delete;
};
This compiles:
int main()
{
std::vector<A> v;
v.emplace_back();
}
But this does not:
int main()
{
std::vector<A> v;
v.emplace(v.end()); // error A is not MoveAssignable
}
However if I changed:
A& operator=(A&&) = delete;
to:
A& operator=(A&&) = default;
then both examples would compile (and run fine).