Skip to content

Commit

Permalink
fix: operator*(M, U u) fixed for U being scaled_unit
Browse files Browse the repository at this point in the history
  • Loading branch information
mpusz committed Oct 25, 2024
1 parent af18a6b commit 9c7d3b0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/core/include/mp-units/framework/unit.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,12 @@ struct unit_interface {
{
if constexpr (std::is_same_v<M, std::remove_const_t<decltype(mp_units::mag<1>)>>)
return u;
else
else if constexpr (is_specialization_of_scaled_unit<U>) {
if constexpr (M{} * U::mag == mag<1>)
return U::reference_unit;
else
return scaled_unit<M{} * U::mag, std::remove_const_t<decltype(U::reference_unit)>>{};
} else
return scaled_unit<M{}, U>{};
}

Expand Down
10 changes: 10 additions & 0 deletions test/static/unit_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,16 @@ static_assert(is_of_type<m_2, scaled_unit<mag<2>, metre_>>);
static_assert(is_of_type<get_canonical_unit(m_2).reference_unit, metre_>);
static_assert(get_canonical_unit(m_2).mag == mag<2>);

constexpr auto m_3 = mag_ratio<1, 2> * m_2;
static_assert(is_of_type<m_3, metre_>);
static_assert(is_of_type<get_canonical_unit(m_3).reference_unit, metre_>);
static_assert(get_canonical_unit(m_3).mag == mag<1>);

constexpr auto m_4 = mag_ratio<1, 2> * (mag<4> * metre);
static_assert(is_of_type<m_4, scaled_unit<mag<2>, metre_>>);
static_assert(is_of_type<get_canonical_unit(m_4).reference_unit, metre_>);
static_assert(get_canonical_unit(m_4).mag == mag<2>);

constexpr auto km_2 = mag<2> * kilometre;
static_assert(is_of_type<km_2, scaled_unit<mag<2>, kilo_<metre_>>>);
static_assert(is_of_type<get_canonical_unit(km_2).reference_unit, metre_>);
Expand Down

0 comments on commit 9c7d3b0

Please sign in to comment.