How does copy-and-swap provide assignment safety, and what performance or design trade-offs does it have?
Show answer and explanation
Copy-and-swap takes or constructs a temporary containing the proposed new state, then commits with a non-throwing swap. If creating the temporary fails, the target is untouched; successful swap also handles self-assignment without a separate branch. The idiom is compact and can unify copy and move input through a by-value parameter, but it may allocate or create an extra temporary, discard reusable target capacity, and produce a move-assignment path that is less efficient or has a weaker noexcept declaration than a tailored implementation. Rule-of-Zero members remain preferable when possible.
Copy-and-swap takes or constructs a temporary containing the proposed new state, then commits with a non-throwing swap. If creating the temporary fails, the target is untouched; successful swap also handles self-assignment without a separate branch. The idiom is compact and can unify copy and move input through a by-value parameter, but it may allocate or create an extra temporary, discard reusable target capacity, and produce a move-assignment path that is less efficient or has a weaker noexcept declaration than a tailored implementation. Rule-of-Zero members remain preferable when possible.
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.