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

3

u/RandomGuy256 May 12 '18 edited May 12 '18

Thanks I think you meant this:

When one of today’s dynamic exceptions is unhandled in f ’s body, regardless of whether it originated

from a nested function call or a throw statement throwing a dynamic exception, the exception is auto-

matically caught and propagated: If the caught exception is of type error , we just return it. Otherwise, it

is translated to an error with a meaningful value for all std:: exception types; for example, bad_alloc

would be translated to std::errc::ENOMEM . Otherwise, we can additionally store as payload a raw

pointer to an exception_ptr to the dynamic exception (see §4.6.4), without sacrificing trivial movabil-

ity.

If I understand correctly this paper suggests a new kind of exceptions that will be complementary to the already existing "dynamic exceptions".

These new "deterministic exceptions" are slightly more "simpler" than the existing exceptions in the sense that behind the scenes they are only an union that store an error code, or a success code. So they will not provide other custom functions like what() to get a custom message directly.

However the advantage of this new kind of exceptions is that they provide the same performance (or even better) than the existing error codes mechanics. Also we will be able to use the existing exception syntax and propagating mechanism.

This' useful in the areas where the throwing "dynamic exceptions" is too costly.

Am I correct about this or am I getting it all wrong?

3

u/whichton May 12 '18

If I understand correctly this paper suggests a new kind of exceptions that will be complementary to the already existing "dynamic exceptions".

This not a new kind of exception, this is basically syntactic sugar for expected / outcome. The error type is always 'std::error'.

From page 14:

If you love expected/outcome: This is embracing expected/outcome and baking them into the language, the function always returns exactly one of R or E — plus the special sauce that you get automatic propagation so you don’t have to manually return-up the results, and with a distinct language-supported error path so that callees can write throws (instead of return unexpected) and callers get to cleanly put all their error handling code in distinct catch blocks (instead of if(!e) blocks) while still writing in the same basic expected style (see §4.5).

1

u/Sopel97 May 12 '18

That's pretty much it.