Back to blog
6 min readBy VIBPS

What are C++26 contracts? Preconditions, postconditions and assertions

C++ contracts let a function state what callers must provide, what it promises to return and which conditions should hold inside its body. Here is how pre, post and contract_assert differ, how evaluation modes affect them, and what teams should know about C++26's still-pending final ballot.

C++Software engineeringProgramming languages

A function signature tells you the types that cross an interface. It usually says less about the values that make sense or the promises an implementation is expected to keep. C++ contracts add a standard way to write some of those conditions next to the function: a precondition for callers, a postcondition for the function, and an assertion at a point inside its body.

The feature is useful as a shared vocabulary for programmers, reviewers and tools. It is not a proof that a program is correct, and a contract check is not necessarily enabled in every build. Those limits matter when deciding whether a condition belongs in a contract, ordinary input validation, a test or a type.

Three places to state a condition

A precondition describes what must be true when a function is called. The caller is responsible for meeting it. For a function that normalizes a range, for example, the precondition might say that the lower bound is no greater than the upper bound. It documents the call boundary and gives an implementation or tool a condition to check.

A postcondition describes a result or state the function promises after it completes normally. Responsibility rests with the function's implementation. It can express a relation between inputs and the result, such as returning a non-negative value when the input was non-negative. A named result can be introduced in the postcondition so the expression can refer to the return value.

A contract_assert statement belongs inside a function body. Use it for a condition expected to hold at that point, such as an internal state-machine invariant after a transition. It can help make an assumption visible where it matters, rather than leaving it in a comment or a private assertion macro.

In the C++26 proposal, these forms use pre(...), post(result_name: ...), and contract_assert(...). The pre and post specifiers are attached to a function declaration; contract_assert is written among the function's statements. The predicate is a Boolean expression. A condition's location communicates who is responsible: the caller, the implementation, or the code path at that point.

A contract is not the same as input validation

A contract describes a requirement or an expected property of a program. It does not automatically turn a bad external request into a safe error response. If an API accepts data from a file, network request or user, the implementation still needs to validate that data and choose a defined response. A precondition can express that callers are expected to pass valid data; it does not guarantee that every caller did so or that a production build will stop when the condition is false.

This distinction also affects security boundaries. Do not rely on a contract as the only barrier before an array access, permission check, database write or other sensitive operation. Keep the runtime checks that make untrusted inputs safe, and use a contract to document an invariant or expectation that is useful to verify during development. Tests and static analysis answer different questions and remain useful alongside contracts.

Why the evaluation mode matters

The C++26 design defines four evaluation semantics: ignore, observe, enforce and quick-enforce. Ignore does not evaluate the predicate. The checking modes evaluate it to determine whether a violation occurred. With observe, a violation handler can run and execution may continue if that handler returns; enforce and quick-enforce are terminating modes. Implementations choose which semantics apply, and the standard's design allows the choice to depend on the implementation and build configuration.

That flexibility means the source code alone does not tell a reviewer that every contract will always stop execution. The expressions also should not have side effects: checking can be configured differently, and evaluation is not a reliable place to perform work. Keep predicates focused on asking whether a condition holds. If a program must reject an input or preserve an invariant in every build, implement that behavior directly in the function.

The November 2026 WG21 meeting added an always-enforced control label to the revised C++26 draft. The syntax lets an author mark a particular precondition, postcondition or assertion so execution cannot continue past it after a violation, regardless of the selected semantics for other assertions. That is a targeted way to express a stronger requirement; it does not make every unlabelled contract always enforced.

What the current C++26 status means

Contracts entered the C++26 working draft through proposal P2900. After the first Draft International Standard ballot did not pass, WG21 revisited comments, retained contracts and adopted an extension at its November 16–21, 2026 meeting. The committee completed technical work again and expects to send an updated draft for a second approval ballot. As of November 22, that second ballot and final ISO publication have not yet happened. Describing the work as included in the revised C++26 draft is accurate; calling the standard finally approved is premature.

Compiler support is a separate question from committee status. Check the compiler version, feature-test macro and documented flags for the toolchain you actually ship. A proposal or a working-draft feature does not guarantee that a given compiler supports the final spelling or semantics. Test the build settings across the translation units and libraries that make up your application.

How a team can start using the idea

  • Start with a public function whose caller obligations or result guarantees are currently scattered across comments and tests.
  • Write one condition with a clear owner: caller responsibility as a precondition, implementation promise as a postcondition, or an internal invariant as contract_assert.
  • Keep validation and error handling in the function where inputs cross a trust boundary; do not make correctness depend on an optional contract check.
  • Avoid side effects in predicates, then verify the chosen compiler's support and evaluation configuration in the actual release build.
  • Use tests to cover expected behavior and failure handling. Contracts can make assumptions visible, but they do not replace those tests or prove the whole function correct.

For makers publishing a C++ library or application, the same principle applies to the product description: state which compiler and language modes you support, and distinguish a documented requirement from a behavior you have tested. VIBPS is a social network for software where makers introduce apps and build an audience around what they create.

Source: WG21 proposal P2900R14: Contracts for C++ (syntax, responsibilities and evaluation semantics)

Source: Herb Sutter: November 2026 C++ standards meeting trip report (C++26 ballot and updated draft status)

Source: WG21 proposal P4451R2: Early Adoption of an Always-Enforced Label

Related coverage: Copilot CLI adds C++ code intelligence for MSBuild

Introduce your C++ app or library on VIBPS

Built around one question: what happens when software becomes easy for almost anyone to create?

Explore apps