contract_assert checks one statement, and a build flag decides what a failure does
std::vector<int> v{10, 20, 30};
std::size_t i = 5;
contract_assert(i < v.size());
int x = v[i];
The first line after the declarations is a C++26 contract assertion. It is a statement. When the predicate is false, the implementation does what the evaluation semantic of the build says. The access on the next line is then either never reached or reached on purpose.
In short
- A contract assertion is a statement that checks one condition where you write it, for example in front of a vector access.
- On GCC 16.2 the same source ignores, reports, or terminates on a failure, depending on
-fcontract-evaluation-semantic. A debug and a release build differ by one flag. - With the report-only semantic at
-O2, the build reports the violation and then goes on to read out of range. - The libstdc++ assertion macro
_GLIBCXX_ASSERTIONSis a separate mechanism. It never calls the contract violation handler. - Clang’s status page lists contracts as not supported, so the demos are GCC only.
Where this fits in the series
This post is the second in a series about checking undefined behavior one statement at a time. A reader question started the series, in a comment from Zamfir Yonchev. The first post, catching undefined behavior with constexpr, answered it with the tool the language already has. Contracts themselves are covered in C++26 contracts and the contracts dispute, and the library-side check in the hardened standard library. This one is about the contract assertion (contract_assert) as the per-statement tool.
Running it
The demo asserts that the index is smaller than the vector size, once for i = 1 and once for i = 5. It uses the observe semantic, so it reports the violation and carries on:
i = 1 passed the contract_assert
i = 5 passed the contract_assert
contract violation in function int main() at example.cpp:16: i < v.size()
[assertion_kind: assert, semantic: observe, mode: predicate_false, terminating: no]
The violation report goes to stderr and the other two lines to stdout, so the order depends on how the streams are merged. The demo sets stdout to unbuffered. Then, when both streams go to a terminal or one pipe, the report appears after the i = 1 line and before the i = 5 line.
On GCC 16.2 the option -std=c++26 is enough:
- Contracts are on by themselves (
-fcontracts). - The feature-test macro
__cpp_contractsis202502L. - Turning contracts off with
-fno-contractsgives the error'contract_assert' is only available with '-fcontracts'.
With -std=c++23 the keyword is not recognised at all.
One link detail: the default violation handler lives in libstdc++exp. Passing -fcontracts explicitly makes the driver link that library for you. Relying on the implicit default from -std=c++26 does not. Without -lstdc++exp, the link then fails with undefined reference to handle_contract_violation(std::contracts::contract_violation const&). That reference is satisfied either by the library or by a handler you define yourself, shown below.
The four semantics, measured
The same contract_assert_bounds.cpp, built on GCC 16.2 with -std=c++26 -Wall -Wextra and only the semantic changed. The table lists the four spellings the compiler accepts. Two other spellings, ignored and observed, appear in some online documentation. The compiler rejects them with unrecognized contract evaluation semantic.
-fcontract-evaluation-semantic= |
output | exit |
|---|---|---|
ignore |
i = 1 ..., i = 5 ..., nothing else |
0 |
observe |
i = 1 ..., violation report (terminating: no), i = 5 ... |
0 |
enforce (default) |
i = 1 ..., violation report (terminating: yes), terminate called without an active exception |
134 |
quick_enforce |
i = 1 ..., terminate called without an active exception, no report |
134 |
Exit 134 is SIGABRT. Under enforce and quick_enforce the statement after the failed assertion never runs. Building the demo at -O2 with the default semantic gives the same enforce result as the table.
A handler of your own
GCC 16.2 lets a program define handle_contract_violation(const std::contracts::contract_violation&) at global scope, and the linker takes it over the library’s weak one. With this handler in place, custom_handler.cpp needs no -lstdc++exp. It asserts i < 3 with i = 5, and the results are:
observe: custom handler: i < 3 | semantic 2
after the contract_assert (exit 0)
enforce: custom handler: i < 3 | semantic 3
terminate called without an active exception (exit 134)
quick_enforce: terminate called without an active exception (exit 134, handler not called)
ignore: after the contract_assert (exit 0, handler not called)
The numbers are static_cast<int>(cv.semantic()). Under enforce the handler runs and then the program terminates, so a handler that returns does not let an enforce build continue.
Debug and release by flag
The source file does not change between a checked build and an unchecked one. Here is contract_assert_then_access.cpp, which asserts and then really does read v[5], built with GCC 16.2 and each combination of optimisation level and semantic:
| flags | what happened | exit |
|---|---|---|
-O0 ignore |
no contract report, then the libstdc++ assertion __n < this->size() fires |
134 |
-O0 observe |
contract report, then the libstdc++ assertion fires | 134 |
-O0 enforce |
contract report, then terminate | 134 |
-O2 ignore |
v[5] = 0 |
0 |
-O2 observe |
contract report, then v[5] = 0 |
0 |
-O2 enforce |
contract report, then terminate | 134 |
Two of the rows show the same program reaching an out-of-range read and printing 0. Under observe at -O2, the build reports the violation. It then goes on to execute the undefined behavior the assertion was written to prevent.
It is not the library check
The libstdc++ check in the -O0 rows above is the library assertion macro _GLIBCXX_ASSERTIONS. GCC enables it by default at -O0. It is a separate mechanism from the contract assertion. Measured on plain_index.cpp, which has no contract_assert:
| flags | what happened | exit |
|---|---|---|
-O2 |
v[5] = 0 |
0 |
-O2 -D_GLIBCXX_ASSERTIONS |
assertion message naming stl_vector.h:1253, then abort |
134 |
With both together, the contract report is printed for the assertion, and then the libstdc++ assertion aborts the program at v[5]. This was measured with -O2 -D_GLIBCXX_ASSERTIONS -fcontract-evaluation-semantic=observe on contract_assert_then_access.cpp. The library’s message is a plain assertion text, not a contract_violation, and changing the semantic flag does not change what the library check does.
The standard wording for the hardened library is different. [structure.specifications] says two things (eel.is, read 2026-10-07). The contract assertions for a hardened precondition are evaluated with a terminating semantic. In a non-hardened implementation, a violated hardened precondition is undefined behavior.
P3878R1 argues for removing the observe semantic from hardened implementations. A returning handler would leave the program in the undefined behavior the hardening exists to prevent. The paper describes checked-but-not-hardened modes as a vendor extension that cannot claim to be hardened. What GCC 16.2’s libstdc++ does today is what the tables above show: its assertions do not go through the handler.
What contract_assert is and is not
- It is a statement. It checks the predicate you write, at the place you write it. It is not an operator applied to an expression, and C++26 has no expression form such as
v[i] ?? std::out_of_range. - What a failure does is chosen at build time, not in the source. A library author cannot make a caller’s build enforce.
- The check runs on inputs your program actually executes, like any assertion.
Clang’s C++ status page lists Contracts (P2900) as not supported, read on 2026-10-07, so the demos above are GCC only.
What this has not been tested on
Everything above was measured on GCC 16.2.0 (Docker, aarch64) and on Compiler Explorer’s g162 (x86-64). Not tested:
- Clang (the page above lists no support).
- MSVC.
- Other GCC versions, including 16.1, which the older contracts post used.
-O1and-O3.-fhardenedand libc++.- Any build where the violating code is inlined into a caller that is itself checked.
At -O2 the demos were small enough that the compiler may have seen the constant index. A runtime-dependent index was not measured.
Sources:
- P2900R14, Contracts for C++
- P3878R1
- [structure.specifications]
- clang C++ status
- GCC 16.2 behavior measured with the four example files in wrocpp/cpp26-reflection-examples and the Godbolt demo above.