Why does the copy-and-swap assignment operator provide the strong exception guarantee?
Correct answer
B. The old state is replaced only after the copy has been fully constructed
55 views
B. The old state is replaced only after the copy has been fully constructed
55 views
Copy-and-swap first builds a complete copy, an operation that may throw while *this remains untouched, and only then swaps the copy with the current state using a non-throwing swap. If anything fails, the object keeps its original value, which is exactly the strong guarantee. The cost is an extra allocation and copy that a self-assignment check or move assignment could avoid, so it trades performance for safety.
Asked in: NTS, PPSC IT, Campus Interviews
Not sure why the answer is B? Ask here and get an explanation.