How do push_back and emplace_back differ in construction, conversions, exceptions, and invalidation?
Show answer and explanation
push_back inserts an existing element value by copy or move, while emplace_back forwards constructor arguments and constructs the element directly in container storage. Emplacement can avoid a temporary when the caller has separate constructor arguments, but passing an already constructed T usually offers no benefit and may make conversions or an unintended constructor less visible at the call site. Both operations have the same container growth and iterator-invalidation rules, and their exception guarantee depends on allocation plus the element's constructors and move properties. Prefer the form that makes construction intent clear, then measure if it matters.
push_back inserts an existing element value by copy or move, while emplace_back forwards constructor arguments and constructs the element directly in container storage. Emplacement can avoid a temporary when the caller has separate constructor arguments, but passing an already constructed T usually offers no benefit and may make conversions or an unintended constructor less visible at the call site. Both operations have the same container growth and iterator-invalidation rules, and their exception guarantee depends on allocation plus the element's constructors and move properties. Prefer the form that makes construction intent clear, then measure if it matters.
Connect the conclusion directly to the type, lifetime, ownership, overload-resolution rule, or library contract present in the question. State implementation dependence or a violated precondition explicitly instead of merely guessing output.