Skip to content

mp_units::Unit

A concept matching every unit

Synopsis

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

template<typename T>
concept Unit = detail::is_unit<T>;

Description

Satisfied by a named unit defined with named_unit, such as si::metre, by a prefixed unit, such as si::kilo<si::metre>, by the unnamed unit a unit equation produces, such as si::metre / si::second, and by a physical constant defined with named_constant.

// the raw number a protocol field holds, in the unit and the type the caller names; a
// conversion that would lose information doesn't compile
template<Unit auto U, typename Rep>
constexpr auto encode(QuantityOf<get_quantity_spec(U)> auto q)
  requires requires { q.template numerical_value_in<Rep>(U); }
{
  return q.template numerical_value_in<Rep>(U);
}

constexpr std::int64_t field = encode<mm, std::int64_t>(2 * m);  // 2000
// doesn't compile: 1.5 km truncates
// auto bad = encode<km, std::int64_t>(1500 * m);
// a trait keyed by unit
template<Unit auto U>
constexpr std::string_view spelled_name = "";

template<>
constexpr std::string_view spelled_name<si::metre> = "metre";
template<>
constexpr std::string_view spelled_name<si::second> = "second";

constexpr std::string_view name = spelled_name<si::metre>;  // "metre"
// doesn't compile; unconstrained, it would key the trait by a quantity
// template<>
// constexpr std::string_view spelled_name<isq::length> = "length";
// values of types nobody spells, so naming what each one is helps where `auto` alone doesn't
constexpr QuantitySpec auto power_spec = isq::energy / isq::duration;
constexpr Dimension auto power_dimension = get_dimension(power_spec);
constexpr Unit auto power_unit = si::joule / si::second;
constexpr Reference auto power_reference = power_spec[power_unit];

See Also