4/30/2026 at 12:55:08 PM
The C++11 example is the weakest in the article by its own thesis. Public throwing constructor, no year check, no leap-year check, so Birthdate(0, 2, 30) constructs cleanly. The C++17/23 shape (private ctor + static factory) is the actual mechanical insight from King's essay. Make the constructor a function that can fail, so the type itself carries the proof.by _alphageek
4/30/2026 at 4:37:17 PM
Just to note, a throwing constructor is “just as good” as static factory method, provided you want to use exceptions for validation errors. Which you shouldn’t, but from the perspective of testing types as proof, it’s just as good.by simonask
4/30/2026 at 12:58:39 PM
exactly, use std::expected as the return type, avoid exceptions, and make a failable factory constructor to build your type. Make invalid states unrepresentable!!!by noitpmeder
4/30/2026 at 1:13:14 PM
Aren't you time-travelling? std::expected is C++23 (so available starting from 2025-2027 xd)by dietr1ch
4/30/2026 at 3:15:52 PM
It has been available since GCC 12.1 (May 2022), Clang 19.1 (Sep 2024), and Visual Studio 17.13 (2022~): https://godbolt.org/z/on1v6qdf3These days compiler developers implement accepted standard features pretty fast.
by diath
4/30/2026 at 6:04:18 PM
And tl::expected (a largely identical impl) has been available similarly as long!by noitpmeder