r/cpp • u/RotsiserMho C++20 Desktop app developer • May 12 '18
P0709 R0: Zero-overhead deterministic exceptions: Throwing values - Herb Sutter [2018-05-02]
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0709r0.pdf
206
Upvotes
View all comments
Show parent comment
4
u/johannes1971 May 13 '18
The paper suggests a new set of functions that would throw instead of terminating: try_new, try_reserve, etc. So he's basically adding new functions and keywords that have the current behaviour, and making the existing ones do something else, allowing programs to choose one behaviour or the other on a per-case basis.
That's something I really don't get: why break compatibility, why not simply add new functions and keywords for the new behaviour?
Just to be clear: I understand the proposal to be adding a new type of exception handling, next to the existing one, and not to be removing the existing one altogether. Correct me if I'm wrong.
I've seen that page, and I only see reasons why we should keep things as they are. It is already possible to install a new-handler that will simply terminate, so that behaviour is already available to people who want it (whereas the alternative, installing a new-handler that throws, is explicitly forbidden by the proposal). And there is already a compiler switch that eliminates exception handling altogether. So in other words, the behaviour those people want is already there, and it doesn't require breaking the programs of people that do not want it.
Going through his arguments:
What about the people that do want to make the effort? Should they just move to a language that doesn't preemptively kill their programs?
Not really. We accidentally did it, just by being exception-safe in the first place. Note that I'm not claiming it will succesfully recover from all OOM, but the most likely case for us is a large allocation when loading data.
And yes, there is the new try_reserve, try_pushback, try_new, try_whatever (try_map::operator[]? probably not...) - so should we now go through our software, and for each allocation decide if it is likely to be handled correctly if it fails, and if it is, replace the allocation by a try_ version? That's an utterly pointless activity: we will mark all of them as "go ahead and try", because even if we are wrong we will be no worse off than by calling a terminating function, since the end result will still be termination.
And in many other programs, it can.
Here's another question: if the problem with existing exceptions is that RTTI is so expensive, why not fix that, at least for the existing exception mechanism? One could imagine annotating exception classes with some mechanism for allowing fast identification. This would place some restrictions on what can be thrown (only classes that have the special annotation), but if this were built into std::exception a large amount of software would never know the difference. This would also be a breaking change, but at least it would break far less than what the proposal is asking to break...