CppCon 2019: De-Fragmenting C++

Herb Sutter

  • add things (more); fix things (some); simplify (least);
  • Why not reverse this?
  • adding things (eg MetaClasses) can simplify things

A tale of two compiler switches

-fno-exceptions
-fno-rtti

  • Make things static by default
  • Dynamic by opt-in

Exception Handling

  • isocpp.org 2018-02 survey; 48% ban exceptions either fully or partially (22% not allowed)
  • error codes; expected/outcome types; exceptions
  • std::error_code. std::filesystem supports both dialect

Root cause

  • Today’s exception handling is not zero-overhead. Just enabling increases size
  • throwing an exception not statically boundable space and time overhead. Violates you can’t reasonably write it better by hand
  • Lack of control

Key definition

  • what is a recoverable error
  • A function cannot do what it is supposed to do. Mirriam webster definition of “error”
  • A precondition violation is always a bug in the caller

Four coordinated proposals

  • Enable zero-overhead exception handling
  • throw few exceptions (90% of all exceptions should not be)
  • support explicit try for visble propagation

static exceptions

  • throwing values of static types means by value. No dynamic memory
  • isomorphic to error codes
  • share return channel
  • Need to be backward compatible. Opt in
  • As-if returning union(Success; Error;} + bool
  • doubles down on value semantics

Proposal

  • Add a noexcept-querable allocator proprety for “reports vs aborts” on allocation failure.
  • recommend conditional noexcept based on the relvant allocator
  • babb on GitHub.com/HerbSutter

Part 2: Run-time type information

  • Same for exceptions
  • Joe Bialek Wendesday Root cause of CVEs by patch year.
    • Type confusion increasing (e.g. static downcast)
    • static_cast perhaps because RTTI is banned
  • RTTI allowed in 68%

static_cast down-casts

  • Peter Collingbourne
  • Uses vtable ordered in memory so downcast is just a range check
  • down_cast

    5 instructions

  • fads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.