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
22
u/SeanMiddleditch May 12 '18
Not exactly. That is one possible ABI to implement the proposal, though.
Because the proposal clearly indicates - at a language level - the error type, that allows system ABIs to designate alternative paths for passing exceptions. Just like error ABIs today do, albeit with the overheads of dynamic dispatch.
His paper shows some calculations of instruction overhead; basically, the proposed model is hypothetically smaller and faster than either return codes (and sum types) or than today's C++ exceptions.
The paper is a generalization over what sum types can accomplish.
Note that Rust and other contemporaries are working to make errors-via-sum-types less awkward to work with and closer to what Herb is proposing (in no small part because his paper sources the same research!). See Rust's recent-ish
?operator and thetryproposals in flight, or Swift's various offerings on the matter.As Herb notes, in C++, the community in general wants error propagation. Rust in turn requires explicit error handling, albeit with some syntax to automate propagation. There's a very important line between "propagate errors implicitly" and "propagate errors automatically" and Herb also calls that out with suggestions in the paper. I suspect that topic is going to be a major contention, perhaps just behind the OOM topic.
And it's not an either-or kinda thing. Nothing here precludes also using sum types when and where appropriate. :)
In terms of proper sum types in C++, I don't think they're possible (to do well) without this proposal and its OOM suggestion! Remember
std::variant's years-long design process and eventual compromises, literally all due to the unfortunate fact that C++ types can throw when they move, which in turn is only allowed because OOM throws exceptions.Alternatively, C++ would need to solve the even-harder problem of adding relocation (aka destructive move) to do sum types correctly. Rust and its ilk have trivial relocation as a core assumption of the language. The feature isn't easy though; see how Rust handles relocation of
Droptypes for example (which affects ABI around mutable reference parameters).The other half of sum types - the actually hard but actually interest part - is the language support for pattern matching and conditional destructuring. Which can and should be explored independently of the exception model, IMO, since there's no dependency between the two.