The C++ Type System Is Your Friend
summary
Argues C++ types and templates enforce domain rules with zero runtime cost: concrete examples include whole value pattern year/month/day wrappers, user‑defined literals, a units/quantity system that does compile‑time dimensional arithmetic (prevents mixes like meters vs feet and the Mars Climate Orbiter failure), and policy‑based fixed_string to handle truncation. Uses CRTP and CRTP base templates plus constexpr checks to move validation to compile time; clang -O3 emits identical assembly, GCC shows only minor differences. Concepts improve error messages.
AI-written summary · about
chapters (24)
- 0:00Intro
- 0:55Safe, performant, reusable code
- 3:06(Mostly) typeless programming
- 6:20Machine-typed programming
- 8:47Type-rich programming
- 10:26The miracle of compilation Runtime
- 13:48Physical types
- 16:14Whole Value pattern
- 18:43Templates to the rescue
- 19:56Adding checking of values
- 25:46Reuse through client libraries
- 33:24Physical quantities and dimensions
- 35:31Tailoring operations - library code
- 37:12Tailoring operations - client code
- 39:47Templates and policies
- 42:38Implementing policies • Let's use a policy on overflow
- 44:18Constructor validation logic • Let's use a policy to enforce that quantities are non-negative
- 45:49Effect of constructor validation logic . So, what about the generated code?
- 46:49Starting to define a domain type . We have now restricted both the operations and the range of allowed values
- 47:32Domain types vs primitives • Business rules still exist when using primitives: they are just distributed
- 54:28Compile-time range calculation
- 55:21Scaling and errors • As systems get larger the number of possible combinations of types (and therefore incompatibilities) grows even faster
- 56:39Templates and "concepts lite"
- 59:15Concepts code - first attempt
related talks
51mThe C++ Type System is your Friend
01h 30mRethinking the Way We Do Templates in C++
01h 13mType safe C++ – LOL! :-)
58mHow to Build Cpp Interfaces That Are Hard to Use Incorrectly
54mTypical C++, But Why?
01h 37mRethinking the Way We Do Templates in C++ even more
57mIntroduction to C++ Template Metaprogramming
50mType safe C++? LOL!
