How does copy-on-write work, and why is it usually unsuitable for modern std::string-style value types?
Show answer and explanation
Copy-on-write shares an internal representation across logical copies and detaches by making a private copy before mutation. It can save copying for read-mostly values, but each write needs reliable uniqueness and synchronization, while references, pointers, and iterators obtained before detachment make ordinary mutable value semantics difficult to preserve. Modern basic_string requirements and concurrency expectations make classic invisible COW implementations impractical for standard strings. Explicit immutable or persistent data structures can still use sharing effectively because their API exposes the lifetime and mutation model instead of pretending every copy is independent and cheaply mutable.
Copy-on-write shares an internal representation across logical copies and detaches by making a private copy before mutation. It can save copying for read-mostly values, but each write needs reliable uniqueness and synchronization, while references, pointers, and iterators obtained before detachment make ordinary mutable value semantics difficult to preserve. Modern basic_string requirements and concurrency expectations make classic invisible COW implementations impractical for standard strings. Explicit immutable or persistent data structures can still use sharing effectively because their API exposes the lifetime and mutation model instead of pretending every copy is independent and cheaply mutable.
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.