# C++26 Contracts Move Assertions Into Function Signatures

Original title: C++26 Contracts: Introduction to Preconditions and Postconditions

> Hacker News story 50103164 · language: en · submitted 2026-10-15
> Web version: https://zeli.app/story/50103164
> About this feed: https://zeli.app/llms.txt · Agent guide: https://zeli.app/skill.md

▲ 145 · by [ibobev](<https://news.ycombinator.com/user?id=ibobev>) · 💬 171
[original](<https://www.cppstories.com/2026/cpp26_contracts/>) · [HN discussion](<https://news.ycombinator.com/item?id=50103164>)

## Summary

C++26 contracts let you attach preconditions and postconditions directly to function declarations, making requirements visible in the API instead of buried in implementation. The article walks through replacing assert with pre and post, covers evaluation semantics like ignore, observe, enforce, and quick-enforce, and shows GCC 16 compilation flags. It also warns that contracts are not input validation and that predicates must avoid mutation.

## Key quote

> Importantly, this aspect of Contracts is why contract assertions must not be used for error handling and input validation: If a function has in-contract requirements to report certain events as errors, that handling must be done with standard C++ control statements that are not optional, never with contract assertions.

## HN discussion (171 comments)

### [cogman10](<https://news.ycombinator.com/item?id=50104290>)

> I don't like it.
>
> I'm all for signing up for a contract if it can turn a runtime error into a compile time error.  However, this looks like it just makes more runtime errors.  At that point, these seem no better than `assert` except for the fact that they promise to really assert (probably).
>
> The article also points out a big problem which is "Importantly, this aspect of Contracts is why contract assertions must not be used for error handling and input validation: If a function has in-contract requirements to report certain events as errors, that handling must be done with standard C++ control statements that are not optional, never with contract assertions."
>
> That sort of muddy definition of what contracts are supposed to be used for is almost certainly going to make a mess.  It's not input validation, but parameter validation.. but not really validation but rather checks which error out, but it's not errors we are producing.

### [WalterBright](<https://news.ycombinator.com/item?id=50108644>)

> I used to be a big fan of contracts, and even implemented it in the Zortech C++ compiler back in the 90's:
>
> https://www.digitalmars.com/ctg/contract.html
>
> I implemented it in D as well. But, it just turned out to not be very useful.

### [ZuLuuuuuu](<https://news.ycombinator.com/item?id=50103875>)

> This reminds me the "Design by Contract" concept from Eiffel programming language:
>
> https://en.wikipedia.org/wiki/Design_by_contract
>
> https://www.eiffel.com/values/design-by-contract/introductio...

### [Surac](<https://news.ycombinator.com/item?id=50109314>)

> What are they even good forbid we can’t validate the input with? A convenient way to write down parameter ranges. Nothing more

### [gavinray](<https://news.ycombinator.com/item?id=50104578>)

> Random tidbit: I wrote a GCC compiler plugin years ago to add Contracts to C/C++ while waiting for this to make it into the standard
>
> https://gavinray97.github.io/blog/adding-invariant-to-cpp-de...
>
> The post turned out to be more useful for an intro to writing GCC plugins than anything, but at least I had fun

### [tsukikage](<https://news.ycombinator.com/item?id=50104624>)

> The article makes no mention of optimisation use cases. In case anyone already knows off the top of their heads and before I start digging for the answer, is part of the intent here for these to inform the optimiser stages, perhaps similarly to what the if (!(some_condition)) { __builtin_unreachable(); } idiom can achieve?

### [MetaverseClub](<https://news.ycombinator.com/item?id=50108749>)

> I wonder what are the use cases in their mind when designing this.
>
> For runtime assertions and documentations, yeah, it would work but it works without contract arguably ok.
>
> For formal verification, the design is way off.

### [randyrand](<https://news.ycombinator.com/item?id=50103835>)

> Maybe just me, but huge complicated languages like C++ seem as unappealing as ever.
>
> If I’m going to write code by hand, the requirement for a beautiful pleasant language is bigger than ever.

---

## Cite

- Page: https://zeli.app/story/50103164
- Date: 2026-10-15
- Source: cppstories.com (https://www.cppstories.com/2026/cpp26_contracts/)
- Hacker News: https://news.ycombinator.com/item?id=50103164

## Other languages

- zh: https://zeli.app/zh/story/50103164 · markdown: https://zeli.app/zh/story/50103164.md
- ja: https://zeli.app/ja/story/50103164 · markdown: https://zeli.app/ja/story/50103164.md
- ko: https://zeli.app/ko/story/50103164 · markdown: https://zeli.app/ko/story/50103164.md
- de: https://zeli.app/de/story/50103164 · markdown: https://zeli.app/de/story/50103164.md
- es: https://zeli.app/es/story/50103164 · markdown: https://zeli.app/es/story/50103164.md
- ru: https://zeli.app/ru/story/50103164 · markdown: https://zeli.app/ru/story/50103164.md
