r/cpp • u/Clean-Upstairs-8481 • Aug 22 '26
C++26 Contracts: What Do They Add Beyond Manual Checks and Assertions?
https://techfortalk.co.uk/2026/08/22/cpp26-contracts/This post is a part of my C++26 exploration series where I take a new feature and try to understand and explain with a simple example in hand. Today’s topic is Contracts. First we will simply try to understand what is the problem it is solving then try doing some assessment on the value addition.
55
Upvotes
20
u/JVApen Clever is an insult, not a compliment. - T. Winters Aug 22 '26
Sure, if you compare to manual if-statements, contracts brings a lot. I however hope that this is not how people are writing their code. Instead, a comparison with assertions would be much more relevant as this is basically a replacement for cassert and all custom variants. This reduces the added value of contracts to: asserts in function declaration, specialized syntax and consistency across libraries.
8
u/Kazppa Aug 22 '26
Terminating my entire program because a write to a buffer failed is a bit too aggressive.
The contract is indeed cleaner but doesn't do the same behavior as the initial exemple.
2
u/all_is_love6667 Aug 22 '26
ah thanks, a first intro to how it would look like in code
it feels a bit like exceptions, except they're more sophisticated and more advanced
I thought contracts were like compile time conditions for templates, but apparently it's even more than that
2
u/CorrodedX Aug 26 '26
I'm not personally a fan of adding even more to the function header. Once you add in templating, constraints, nodiscard, etc, it starts to get unmanageable.
I'd have very much preferred to see something in the function body, like static_assert, only runtime... like "assert". Which then makes me wonder what we're really gaining here if assertions and exceptions already exist.
4
u/pjmlp Aug 22 '26
To really understand what contracts are in general, from programming language point of view, and not specific to C++, it is worth looking into their use in production systems.
Meaning, Eiffel (which introduced them in first place), Ada/SPARK, Frama-C.
Then you have D, which I wouldn't assert there is much use of its contracts feature in production, let alone the language, unfortunely.
Finally, one can get esoteric by elevating the contracts into the type system, and thus formal proof and dependent type languages like Roc, Lean, Dafny, Idris, FStar,.... that keep language researchers busy, especially now where they are researching how they can combine them with agentic tools.
Thus we can after get such an overview, consider how much of that is achieveable with C++26 contracts.
1
u/usefulcat Aug 28 '26
This line of the first version of the write() method seems wrong:
It fails to account for the 4 header bytes that were also written. The version under "The contracts way" doesn't have this problem.
-13
u/ArsonOfTheErdtree Aug 22 '26
Contracts are sort of implicit rust-traits. They tradeoff explcitiness for backward compatibility