Get familiar with contracts that may become a part of C++23.

It is unknown whether or not contracts, reflection, or pattern matching will be part of C++23. The general idea is that they should be part of an upcoming C++ standard. This means that they are partially supported in C++23.

Contracts in C++

Contracts were planned to be the fifth big feature of C++20. Because of design issues, they were removed in the standardization committee meeting in July 2019 in Cologna. At the same time, the study group 21 for contracts was created.

A contract specifies interfaces for software components in a precise and checkable way. These software components are typically functions and member functions that have to fulfill preconditions, postconditions, or invariants. Here are the simplified definitions of these three terms:

  • A precondition: a predicate that is supposed to hold upon entry in a function

  • A postcondition: a predicate that is supposed to hold upon exit from the function

  • An assertion: a predicate that is supposed to hold at its point in the computation

The precondition and the postcondition are placed outside the function definition, but the invariant (assertion) is placed inside. A predicate is a function, which returns a boolean.

Here is a first example:

Get hands-on with 1200+ tech skills courses.