3 ms·
Could the C++ standard have fixed this? Sure – the C++ standard goes into great detail about when iterators and references into strings are invalidated, and all
by thras 17y ago
Could the C++ standard have fixed this? Sure – the C++ standard goes into great detail about when iterators and references into strings are invalidated, and all it would have taken would be to add the copy constructor and operator=() to the list of reference-invalidating functions (section 23.1.5, for those who are following along in the standard). A second option would have been to declare that operator=() invalidates existing references for writing, but not for reading. But the standards committee preferred simplicity, convenience and safety to performance – uncharacteristically, if you’ll forgive a smidgen of editorializing.
Yes, it was uncharacteristic...but good god, imagine the chaos if your references were invalidated by any copy or assignment operation. I don't even want to think about the consequences for complex code. The standard committee was exactly right to choose the (avoidable if you're careful) performance hit over the complex "fix."
I disagree with his fix of casting non-const strings to const for a quick fix. The best way is to use const in the first place for any strings you don't plan to write to.