|
Unit Conversion and Dimensional Analysis Library 3.6.1
A compile-time, header-only C++23 dimensional-analysis library
|
The umbrella header <units.h> pulls in every dimension. If a translation unit needs only a few, include the per-dimension headers instead — you pay compile time only for what you use.
Everything — one include, heavier:
Only what you need — lighter. Each dimension has its own header under units/:
A per-dimension header is self-contained (it includes the core machinery it needs), so any of them compiles on its own.
Multiplying or dividing quantities produces a new kind: meters / seconds is a velocity, mass * acceleration is a force. Each result kind has its own header (<units/velocity.h>, <units/force.h>). Including the header the result lands in is a best practice, not a requirement.
With the result's header included:
Without it:
Skipping a header does not corrupt a value or a dimension. It changes the result's name — its printed form, its compiler messages, and dispatch that keys on the name.
To make code that dispatches on a result independent of the header set, key it on the dimension concept rather than the concrete named type. Every dimension has one — units::Velocity, units::Force, units::Length — and a concept classifies by dimension, so it matches the same result whether or not the named header was included:
For the full account of the type divergence and what stays safe, see naming computed results consistently.
The library is heavily templated, so translation-unit compile time scales with how much of it you instantiate. Restricting the includes to the dimensions a file actually uses keeps that file's compile time down; it does not change run-time behavior or code size (unused templates are never instantiated). For the broader performance picture, see efficiency.
Caveat — the constants live in the umbrella header. units::constants (c, G, h, …) is defined in <units.h>. If you need a physical constant, include the umbrella header; the per-dimension headers do not provide the constants.