4 ms·
Nothing about this problem seems unique to C++ or even made more difficult by C++. In most languages it's a challenge to ensure objects stay in a valid state if
by kllrnohj 4y ago
Nothing about this problem seems unique to C++ or even made more difficult by C++. In most languages it's a challenge to ensure objects stay in a valid state if an exception is thrown in the middle of a series of mutations. But that's also the type of problem that you very rarely need to tackle. It's not like you're making a transactional system with rollbacks every time you want to go poke a few swing gui elements, after all.
- layer8 4y agoRight. But an object being in an inconsistent/invalid state can have more serious consequences in C++ than in other languages, due to the weak typing and manual-by-default memory management. In other languages, the programs may be just as incorrect, but the consequences, when the fault arises, tend to be less fatal. Of course, such a fault can in principle still lead to lost or corrupted data, but that tends to be lesa visible than a segfault.
- kllrnohj 4y agoThat'd only be the case if you're not using things like unique_ptr / shared_ptr. Or if you have a really funky entangled ownership model such that a split-copy ends up leaving you in a confused ownership model such that the destructor doesn't do the right thing. And you didn't catch the exception to patch that up before propagating it. That seems like a bit of a stretch to come up with hitting such a scenario, and if you do it doesn't seem like a copy & swap idiom is really the solution.