Skip to content

mp_units::zero

Zero of any quantity and of any quantity-like type

Synopsis

Declared in <mp-units/framework/zero.h>

inline constexpr zero_t zero = zero{};

Description

For a quantity that isn't a point, zero is the only value whose meaning doesn't depend on the unit. A comparison against it needs no unit, and writing zero instead of a bare 0 shows that the unit is omitted on purpose:

quantity<si::metre> total = zero;
for (quantity leg : {3. * km, 0.5 * km}) total += leg;

if (total > zero) std::println("travelled {}", total);

The same comparison works, once mp-units/systems/si/chrono.h is included, for a std::chrono::duration, so generic code doesn't have to tell them apart:

template<typename T>
bool is_positive(const T& value) { return value > zero; }

The value of zero is part of its type, so it survives being passed through a function: std::less<>{}(q, zero) and std::ranges::find(v, zero) work for quantities and for std::chrono::duration. zero initializes both. It doesn't compare against or convert to a plain number such as double, so it never stands in for one. A quantity point can't be compared against zero, because its numerical value depends on the origin. Compare its delta from a named origin instead.

See Also