A new unit in one line
In October 1958, pledges of an MIT fraternity measured the Harvard Bridge by repeatedly laying down their shortest member, Oliver Smoot, and marking the pavement. The result, 364.4 smoots “plus one ear”, is still repainted on the bridge today. One smoot is exactly 5 feet 7 inches, Oliver’s height, or 1.7018 m. (Smoot later became chairman of ANSI and president of ISO, which is either irony or destiny.)
The smoot is a good stress test for a units library, because no library ships it. Real codebases are full of smoots: ticks, lots, pips, sectors, TEUs, story points, all units your domain invented. If adding one is painful, the library gets fought instead of used.
One line
In mp-units, a new unit is a declaration:
inline constexpr struct smoot final
: named_unit<"smoot", mag_ratio<17'018, 10'000> * si::metre> {} smoot;
That single line states the unit’s name, its printing symbol, and its exact magnitude as a rational factor of the metre. There is no floating-point conversion constant to accumulate error. Everything else is immediately available, because the rest of the library is defined in exactly the same way; the SI units are not special cases baked into the engine.
The demo defines the smoot, states the bridge’s length in it, converts to metres (620.136 m, which you can check against the plaque), and then divides by a crossing time to get a speed in km/h. That division makes the brand-new unit compose with the whole system. Derived quantities, kind checking from episode 2, and the affine machinery from episode 3 all apply to your unit the moment it exists.
The same mechanism scales up: whole systems of quantities (the SI itself, natural units, IEC data units) are built from these declarations, and your domain’s system can sit beside them as an equal.
Getting a value back out is the next problem: std::format does not yet know how to print a smoot, and I want the unit to travel with it all the way to the log line.
Sources: mp-units documentation: systems of units · MIT Museum on the Smoot.