Home

emplaceback

Emplaceback is a member function of standard library sequence containers that constructs a new element at the end of the container in place, using the provided arguments as constructor parameters. Introduced in C++11, it constructs the element directly within the container’s storage, avoiding the creation of a temporary object.

Mechanism: Emplaceback uses perfect forwarding to forward the given arguments to the element type’s constructor. This

Usage: Typical usage is to call emplace_back with the constructor arguments of the element type. For example,

Comparison with push_back: push_back adds an existing object to the end of the container, possibly requiring

Notes and caveats: Reallocation of the container can move existing elements, so performance depends on the

Availability: Emplaceback is provided by standard sequence containers such as std::vector, std::deque, std::list, and std::forward_list, and

enables
in-place
construction
and
can
reduce
or
eliminate
copies
or
moves
compared
with
push_back,
which
typically
requires
a
pre-constructed
object.
for
a
vector<Point>
where
Point
has
a
constructor
Point(int
x,
int
y),
you
can
write
v.emplace_back(3,
4).
a
copy
or
move.
Emplaceback
builds
the
object
directly
from
the
provided
arguments,
which
can
be
more
efficient
for
complex
or
non-copyable
types.
move/copy
behavior
of
the
element
type.
If
the
type
has
a
nonnoexcept
move
constructor,
reallocation
may
be
more
expensive.
As
with
other
in-place
constructions,
you
should
ensure
the
provided
constructor
arguments
are
valid
for
the
element
type.
is
part
of
the
C++11
standard
and
later.
It
is
commonly
used
to
construct
elements
directly
in
the
container
in
scenarios
where
creating
a
temporary
object
would
be
undesirable.