Programming with Contracts in C++20
summary
C++20 adds contract attributes (examples: expects/ensures/assert attributes) but with important limits: postconditions cannot refer to the pre-call state (no general old), invariants are effectively absent, function pointers and callbacks cannot carry contracts, and virtual overrides must repeat the same contract list. Checks run where the function is defined (contracts checked inside callee), build levels (OFF/DEFAULT/AUDIT) are per translation unit, violation handlers exist and by default lead to terminate. A Clang fork proof‑of‑concept implements this and demonstrates the behaviors and pitfalls.
AI-written summary · about
chapters (22)
- 0:00Intro
- 0:15Welcome
- 1:22What are contracts
- 3:50How I learned about contracts
- 4:40Systematic approaches to program construction
- 6:57Ring Buffers
- 9:00Precondition
- 10:58Preconditions
- 17:52Postcondition
- 24:46Exceptions
- 26:04Post Conditions
- 26:46Contract Logic
- 29:30Invariants
- 30:02Portrait specialization
- 31:42Specializations
- 32:53Subcontractors
- 34:10Why bother
- 35:24Contract Violations
- 39:26Science Fiction
- 39:53The Draft Standard
- 41:01Contract Attributes
- 48:41Contract Attributes Example
related talks
01h 34mContracts programming after C++17
01h 03mCpp Contracts - How Contracts in C++26 Can Improve C++ Code Safety and Correctness
01h 31mContracts for C++
01h 02mContracts in C++26 - An Insider's Overview
01h 22mContracts, Safety and Correctness in C++
01h 32mContracts for C++
01h 17mFunction Contracts in Practice using C++
01h 30mContracts, Safety, and the Art of Cat Herding
