A survey of C++ unit libraries that use the type system to enforce dimensional correctness at compile time with zero runtime overhead. Covers user-defined literals (UDLs) and their limitations, then compares four libraries: PhysUnits-CT-Cpp11, SI, Au, and mp-units. Au (C++14+) is highlighted as a practical, lightweight choice using arithmetic composition with unit constants, while mp-units (C++20+) is the most feature-complete and serves as the basis for the upcoming C++ standard proposal P3045, targeting C++29. Key concepts include dimensional analysis, quantity kinds (distinguishing semantically different quantities with the same dimension), QuantityPoint vs Quantity (mirroring chrono's time_point/duration), and safe implicit conversion rules. The article also previews the ongoing standardization effort to add physical units to the C++ standard library.

21m read timeFrom accu.org
Post cover image
Table of contents
Type safety and user-defined literalsUnit libraries using UDLsUnit libraries without UDLsA standard unit library for C++29ReferencesFootnotes

Sort: