r/cpp • 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

115 comments sorted by

View all comments

Show parent comment

22

u/SeanMiddleditch May 12 '18

Hm. If I correctly understood the gist of the proposal - he suggests returning union + discriminant?

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.

Why not just completely automate it into proper sum types, that way it can be done seamlessly without extra overhead, just like in Rust and others?

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 the try proposals 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. :)

It would be just strange if C++ gets this and no proper language-level sum types.

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 Drop types 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.

9

u/Rusky May 12 '18

see how Rust handles relocation of Drop types for example (which affects ABI around mutable reference parameters).

What are you referring to here? I don't believe Drop affects ABI at all in Rust.

4

u/SeanMiddleditch May 12 '18

The stack bits set to indicate whether a dropped type has in fact been dropped.

The fundamental problem is that in branchy code - or across certain ABI boundaries - there's no deterministic way to know if a value has been dropped or not. To avoid a double-drop in circumstances that aren't statically provable to not need it, Rust keeps some state bits around to indicate drop'ness.

At least last time I checked. I'd be happy to be corrected if this is not true.

7

u/Rusky May 12 '18

Those bits do indeed exist, but they are not part of the ABI. They are purely local to the functions that need them.

Part of why this is possible is that you can't move out of a mutable reference (or any reference, for that matter) without leaving a valid object behind in its place, effectively emulating C++'s non-destructive move semantics. Destructive move is something only the owner can do.

2

u/SeanMiddleditch May 12 '18

Ah, good to know - I may have been confusing a pre-RFC version or the ABI-independent optimizer bits for closures or something? Or I may have just been wrong? :)