A contract_assert statement is a contract assertion that may appear in a function or lambda body to verify an internal condition. It ensures the condition holds during execution, triggering a violation (e.g. termination) in debug builds if the condition evaluates to false or the evaluation exits via an exception, and can be ignored in release builds for performance.
[edit] Syntaxcontract_assert
attr (optional) (
predicate )
;
attr - any number of attributes predicate - boolean expression that should evaluate to true [edit] Keywords
[edit] Notes [edit] Example
The contract_assert ensures that vector's norm is positive and either normal or subnormal.
template <std::floating_point T> constexpr auto normalize(std::array<T, 3> vector) noexcept pre(/* is_normalizable(vector) */) post(/* vector: is_normalized(vector) */) { auto& [x, y, z]{vector}; const auto norm{std::hypot(x, y, z)}; // debug check for normalization safety contract_assert(std::isfinite(norm) && norm > T(0)); x /= norm, y /= norm, z /= norm; return vector; }[edit] Support status C++26 feature Paper(s)
GCC
Clang
MSVC
Apple Clang
EDG eccp
Intel C++
Nvidia HPC C++ (ex PGI)*
Nvidia nvcc
Cray
Contracts (FTM)* P2900R14 [edit] ReferencesRetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4