Skip to content

mp_units::UnitOf

A concept matching the units of a given quantity

Synopsis

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

template<typename U, auto QS>
concept UnitOf =
  Unit<U> && QuantitySpec<decltype(QS)> &&
  (mp_units::detail::unit_kind_convertible(get_quantity_spec(U{}), QS) ||
   (unsatisfied<
     "Unit '{}' is associated with quantity of kind '{}' which is not convertible to the '{}' quantity">(
     U{}, type_name(get_quantity_spec(U{})._quantity_spec_), type_name(QS))));

Description

Satisfied by a unit whose quantity converts implicitly to QS. si::metre is a unit of isq::height, while si::hertz is not a unit of isq::activity, which has the becquerel.

// a motion profile whose members all derive their units from the unit of length it is given
template<UnitOf<isq::length> auto L>
struct motion_profile {
  quantity<isq::distance[L]> distance;
  quantity<isq::speed[L / si::second]> speed;
  quantity<isq::acceleration[L / square(si::second)]> acceleration;
};

constexpr motion_profile<km> launch{isq::distance(3. * km), isq::speed(1.2 * km / s),
                                    isq::acceleration(0.03 * km / s2)};
// doesn't compile: not a unit of length
// motion_profile<s> bad;
// the label of a plot axis that shows lengths, in the unit its user picked
template<UnitOf<isq::length> auto U>
constexpr std::string_view axis_label() { return unit_symbol(U); }

constexpr std::string_view label = axis_label<km>();  // "km"
// doesn't compile; unconstrained, it would label the axis "s"
// auto bad = axis_label<s>();
// values of types nobody spells, so naming what each one is and which quantity it belongs to
// helps where `auto` alone doesn't
constexpr QuantitySpecOf<isq::power> auto power_spec = isq::energy / isq::duration;
constexpr UnitOf<isq::power> auto power_unit = si::joule / si::second;
constexpr ReferenceOf<isq::power> auto power_reference = power_spec[power_unit];

See Also