Chapter 14. Lazy Optimization, Part 2: Introducing Laziness

Difficulty: 3

Copy-on-write is a common optimization. Do you know how to implement it?

Original::String (from Item 13) is all very well, but sometimes copies of string objects are taken, used without modification, and then discarded.

“It seems a shame,” the implementer of Original::String from Item 13 might frown to herself, “that I always do all the work of allocating a new buffer (which can be expensive) when it may turn out that I never needed to if all the user does is read from the new string and then destroy it. I could have simply let the two string objects share a buffer under the covers, avoiding the copy for a while, and only really take a copy when I know I need to because one ...

Get More Exceptional C++ now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.