Summary
C++26 Contracts is standard syntax that lets you attach pre (precondition) and post (postcondition) to a function declaration and check conditions inside the body with contract_assert. Unlike the assert macro, the condition appears in the declaration, so callers and tools can see it. What happens when a condition fails depends on the evaluation semantic, and which semantic is used is implementation-defined. GCC 14.2 does not support C++26 Contracts yet. According to the cppreference compiler support table, support starts with GCC 16. This post was prompted by a Hacker News discussion.
What are C++26 Contracts?
One-line answer: Standard syntax where you attach pre (before the call) and post (after return) to a function declaration and check conditions in the body with contract_assert.
According to cppreference, contract assertions consist of function contract specifiers (pre, post) and contract_assert statements, and each has a predicate of type bool. They are checked at these points:
pre: the condition on entry to the function (precondition)contract_assert: a condition checked at that position in the bodypost: the condition when the function returns (postcondition). You can name the return value, as inpost(r: ...).

Here is a sample that uses all three. It does not compile on GCC 14.2, so there is no run output.
#include <cstdio>
int safe_div(int a, int b)
pre(b != 0)
post(r: r * b <= a)
{
contract_assert(a >= 0);
return a / b;
}
int main() { std::printf("%d\n", safe_div(10, 2)); }
How is it different from assert?
One-line answer: assert is a <cassert> macro that lives only inside the body, while Contracts is language syntax attached to the declaration, so callers and tools can see the condition.
When NDEBUG is defined, assert expands to nothing, and because it sits inside the function body you cannot learn the condition from the declaration alone. With Contracts the condition is attached to the declaration, so the caller side and tools see the same condition. That assert is a macro puts it in the same family as the macro pitfall discussed in the expected unqualified-id error post.

Here is the assert version you can use today. It compiled with g++ -std=c++17 assert_alt.cpp -o assert_alt; running it with no arguments first printed 5 from safe_div(10, 2), then the call with b = 0 printed the assert message on stderr and aborted with exit code 134. Below is the real output with stdout and stderr captured to one file. The code comments mean “pre-C++26 style” and “flush right away with endl”.
#include <cassert>
#include <iostream>
int safe_div(int a, int b) {
assert(b != 0 && "b must not be zero"); // C++26 이전 방식
return a / b;
}
int main(int argc, char**) {
std::cout << safe_div(10, 2) << std::endl; // endl로 바로 내보냄
return safe_div(1, argc - 1);
}
$ ./assert_alt
5
assert_alt: assert_alt.cpp:4: int safe_div(int, int): Assertion `b != 0 && "b must not be zero"' failed.
The std::endl is there because of buffering: if output is still sitting in a buffer when the program aborts, it can be lost.
What happens when a condition fails?
One-line answer: Depending on the evaluation semantic, the check is ignored, or a violation handler is called, or the program terminates; which semantic is used is implementation-defined.
cppreference describes four evaluation semantics. Which one is used is implementation-defined and can differ per evaluation.
| Semantic | Checked? | On violation (runtime) |
|---|---|---|
ignore | No (no effect) | Not applicable |
observe | Yes | Violation handler is called; execution continues after it returns |
enforce | Yes + terminating | Violation handler is called; after it returns, contract-terminated |
quick-enforce | Yes + terminating | Contract-terminated immediately, no handler |
- A violation occurs when the predicate is
false, exits via an exception, or (in constant evaluation) is not a core constant expression. - The handler is
::handle_contract_violation(const std::contracts::contract_violation&). Whether it can be replaced is implementation-defined. - Whether contract-terminated means
std::terminate,std::abort, or simply stopping execution is also implementation-defined. - Even with a checking semantic the predicate may not be evaluated, so do not rely on side effects.
If you need a tool for finding bad memory accesses at runtime, see the AddressSanitizer post (Korean).
Can I use it with current compilers?
One-line answer: GCC 14.2 does not support it, and per the cppreference table support starts with GCC 16, so use assert or similar until then.
GCC 14.2 does not support C++26 Contracts yet. According to the cppreference compiler support table, support starts with GCC 16. This is the real output from compiling the sample above with g++ (Debian 14.2.0-19) 14.2.0 on 2026-10-23 KST (exit code 1).
$ g++ -std=c++26 -c contracts.cpp
contracts.cpp:3:5: error: expected initializer before ‘pre’
3 | pre(b != 0)
| ^~~
contracts.cpp: In function ‘int main()’:
contracts.cpp:9:34: error: ‘safe_div’ was not declared in this scope
9 | int main() { std::printf("%d\n", safe_div(10, 2)); }
| ^~~~~~~~
g++ -std=c++2c -fcontracts -c contracts.cpp produced the same output. I also checked the feature-test macro.
$ echo | g++ -std=c++26 -dM -E -x c++ - | grep -c __cpp_contracts
0
The 0 means the __cpp_contracts macro is not defined. According to cppreference, on a compiler that supports the feature this macro is 202502L (C++26).
In the cppreference C++26 compiler support table (checked 2026-10-23), the Contracts (P2900R14) row lists only GCC 16, and the Clang and MSVC cells are empty. I do not guess versions for other compilers.
Until then, use the assert sample above or your own check function. For another piece of syntax that states intent in a declaration, see the C++ explicit post.
What are the frequently asked questions?
Is there a performance cost?
It depends on the evaluation semantic. With ignore, nothing is checked.
Do Contracts replace exceptions?
No. Contracts express bugs and violated assumptions, not recoverable error handling. For returning failure as a value, see the C++ std::expected post (Korean).
How should I migrate existing code?
Keep assert for now, and move the conditions to pre/post once your compiler supports them.
What related posts are worth reading?
- The expected unqualified-id error
- C++ explicit
- AddressSanitizer (Korean)
- C++ std::expected: handling failure without exceptions (Korean)
What are the sources?
- Hacker News: C++26 Contracts: Introduction to Preconditions and Postconditions (2026-10-15 KST) — the discussion that prompted this post
- cppreference: Contract assertions (checked 2026-10-23)
- cppreference: Compiler support for C++26 (checked 2026-10-23)
- Own measurement: g++ (Debian 14.2.0-19) 14.2.0, 2026-10-23 KST