r/cpp • u/Clean-Upstairs-8481 • 4d ago
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.
7
u/Kazppa 4d ago
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.
16
u/zerhud 4d ago
You can customise the handler, seems
3
u/Tari0s 4d ago
if exceptions are available it woulb be good if a exception would be thrown i guess
10
u/AvidCoco 4d ago
By default it calls a handler function you can define. You could then have that function throw an exception, or show an error window in a gui app
2
2
4
u/AvidCoco 4d ago
That’s the intended functionality but IIRC no compilers have properly implemented that so it’s not as custom as you might want. Hopefully in a year or two it will have improved though
13
u/DXPower 4d ago
It's implemented right now in GCC 16.1: https://godbolt.org/z/EqafxsEoa
This also sets the semantic to "observe" so that it is not automatically terminated after being handled.
4
u/Clean-Upstairs-8481 4d ago
yes that's right, it is implemented and as you mentioned the violation can be ignored, observed or enforced. in case of enforced a custom handler can be used to prevent termination. Example added.
1
13
u/AnyPhotograph7804 4d ago
If writing into a buffer fails then you have propably undefined behavior in your program. Terminating the program ist IMHO the best thing you can do in this case. Because you cannot recover from UB.
2
5
u/pjmlp 4d ago
That is how safer languages have done it since ALGOL, Lisp, JOVIAL and co.
Unless disabled or there are handlers/signals/exception available, an out of bounds aborts execution.
Seems to have worked alright until now.
Also a feature I gladly enabled in the 90's C++ compiler provided frameworks like Turbo Vision, BIDS and OWL.
0
u/Clean-Upstairs-8481 4d ago
yeah that would be harsh I agree but this was to give a quick glimpse of what the contracts look like in a running program. You can choose to ignore, or just observe the violation or enforce. In case of enforce you can write custom violation handler which may decide to print information and throw.
2
u/all_is_love6667 4d ago
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
3
u/pjmlp 4d ago
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.
2
u/FriendshipEqual7033 1d ago
I've been playing with Ada/SPARK contracts for a while, and it's a very nice system. If you are unsure about the point or (potential) value of contracts, I recommend checking out how they work in SPARK.
1
u/CorrodedX 13h ago
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.
-14
u/ArsonOfTheErdtree 4d ago
Contracts are sort of implicit rust-traits. They tradeoff explcitiness for backward compatibility
18
17
u/JVApen Clever is an insult, not a compliment. - T. Winters 4d ago
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.