File Contract.h

↰ Parent directory (ADS)

The contracts of Namespace ADS: the preconditions, the postconditions and the invariants of the operations, checked at run time by three macros that mirror the contracts of C++26 (pre, post and contract_assert), so that they can be replaced by them when the compilers support them:

Definition (ADS/Contract.h)

Detailed Description

  • Define ADS_PRE, at the start of an operation, what the caller must ensure (e.g., ! empty() before deleteMin());

  • Define ADS_POST, at the end, what the operation ensures, when it costs no more than the operation itself;

  • Define ADS_INVARIANT, at the end of the operations that change a structure, its invariant (e.g., the order of a heap), which usually costs O( n ) and is meant for the tests;

  • Define ADS_AUDIT, a precondition or a postcondition that costs more than the operation (e.g., a sorted vector before a binary search), checked with the invariants.

The level ADS_CONTRACTS chooses which are checked: 0 none, 1 the preconditions (the default, 0 when NDEBUG is defined, as for assert), 2 also the postconditions, 3 also the invariants and the audits. A violated contract throws Class contract_violation, a std::logic_error whose message names the kind of contract, the condition, the function and the place in the source. The conditions are compiled at every level, so that they stay correct C++, but they are not evaluated above the level chosen; hence they must have no side effects.

  1. Berne, T. Doumler, A. Krzemienski et al., Contracts for C++, P2900R14, ISO/IEC JTC1/SC22/WG21, 2025

  1. Meyer, Applying “design by contract”, Computer 25(10), 40-51, 1992, doi:10.1109/2.161279

Donato Meoli

Dipartimento di Informatica Universita’ di Pisa

by Donato Meoli

Includes

Included By

Namespaces

Classes

Functions

Defines