Friday, September 18, 2009

c++ gotcha




COW for strings in C++:

For those who like their punchlines first: std::string is designed to allow copy-on-write. But once operator[] is called, that string is ruined for COW forevermore. The reason is that it has to guard against a future write at any time, because you may have stashed away the internal pointer that operator[] returns ? a feature that you surely know better than to use, but that costs you nevertheless.