General features: noexcept

code

Syntax

Usage / Usability

  • Declares to the compiler that a given function will NEVER propagate an exception
  • A function declared as noexcept that encounters an exception will immediately terminate the program
  • By default, it occurs as noexcept(true), and is used as noexcept
  • Allows for flexible conditional enabling of the noexcept feature

Very important for generic programming!

The following are great functions to decorate with noexcept:

  • Destructors – these should never throw
  • Move constructors
  • Functions that were not designed to handle exceptions

 

Conditional noexcept

 

The functions are noexcept based on the expressions inside.

PROS

  • noexcept functions are more optimizable than non-noexcept functions.
  • noexcept is particularly valuable for the move operations, swap, memory deallocation functions, and destructors.

CONS

  • noexcept is part of a function’s interface, and that means that callers may depend on it.

Leave a comment

Your email address will not be published.