-
Book Overview & Buying
-
Table Of Contents
C++ STL Cookbook - Second Edition
By :
Scheduled for inclusion in C++26, contracts provide a practical way to make correctness requirements explicit and enforceable. This supersedes many uses of assert and other less enforceable techniques such as comments and coding conventions. Contracts can materially improve both code quality and developer effectiveness.
Preconditions and postconditions state what a function requires and guarantees. This reduces misuse and eliminates guesswork for callers. Additionally, the reader no longer must infer intent from defensive code or documentation. The intent is declared where it matters.
For example:
int divide(int numerator, int denominator)
pre(denominator != 0 &&
numerator % denominator == 0)
post(r : r * denominator == numerator)
{
return numerator / denominator;
}
Now the correctness logic is located in the function itself. When assumptions change, there is a single authoritative place to update them. This should lead to simpler...