mp_units::utility::equivalent¶
Tests whether two units are of the same size
Synopsis¶
Declared in <mp-units/framework/unit_definitions.h>
template<Unit Lhs, Unit Rhs>
[[nodiscard]] consteval bool equivalent(Lhs lhs, Rhs rhs);
Description¶
Two different units may measure the same amount. operator== tells whether the two are the same unit, and this function whether a number means the same in both, which is the case when their canonical units match. Only the units are compared. Hz and Bq are equivalent, although they measure quantities of different kinds. K and deg_C are equivalent too, although their origins differ.
Code that uses a numerical value as it is, without scaling it, needs this test. For example, numerical_value_ref_in returns a reference to the stored number, so it accepts only a unit equivalent to the one of the quantity:
void legacy_update(double& joules);
template<QuantityOf<isq::energy> Q>
requires std::same_as<typename Q::rep, double>
void update(Q& q)
{
if constexpr (utility::equivalent(Q::unit, si::joule))
legacy_update(q.numerical_value_ref_in(si::joule));
else {
double joules = q.numerical_value_in(si::joule);
legacy_update(joules);
q = joules * si::joule;
}
}
Return Value¶
true if a value means the same amount in both units
Note
The return value should not be discarded.
Template Parameters¶
| Name | Description |
|---|---|
| Lhs | the type of the first unit |
| Rhs | the type of the second unit |
Parameters¶
| Name | Description |
|---|---|
| lhs | the first unit |
| rhs | the second unit |