From 9c7d3b0f950326ebb8112e4f68021170c915af70 Mon Sep 17 00:00:00 2001 From: Mateusz Pusz Date: Fri, 25 Oct 2024 12:07:06 +0200 Subject: [PATCH] fix: `operator*(M, U u)` fixed for `U` being `scaled_unit` --- src/core/include/mp-units/framework/unit.h | 7 ++++++- test/static/unit_test.cpp | 10 ++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/core/include/mp-units/framework/unit.h b/src/core/include/mp-units/framework/unit.h index 3624e243..c8d99a9a 100644 --- a/src/core/include/mp-units/framework/unit.h +++ b/src/core/include/mp-units/framework/unit.h @@ -157,7 +157,12 @@ struct unit_interface { { if constexpr (std::is_same_v)>>) return u; - else + else if constexpr (is_specialization_of_scaled_unit) { + if constexpr (M{} * U::mag == mag<1>) + return U::reference_unit; + else + return scaled_unit>{}; + } else return scaled_unit{}; } diff --git a/test/static/unit_test.cpp b/test/static/unit_test.cpp index 2269805e..e42a96df 100644 --- a/test/static/unit_test.cpp +++ b/test/static/unit_test.cpp @@ -253,6 +253,16 @@ static_assert(is_of_type, metre_>>); static_assert(is_of_type); 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); +static_assert(is_of_type); +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, metre_>>); +static_assert(is_of_type); +static_assert(get_canonical_unit(m_4).mag == mag<2>); + constexpr auto km_2 = mag<2> * kilometre; static_assert(is_of_type, kilo_>>); static_assert(is_of_type);