From e6eb9085039119be4994dd8ef262be80515d1de3 Mon Sep 17 00:00:00 2001 From: Mateusz Pusz Date: Mon, 3 Apr 2023 17:44:34 +0200 Subject: [PATCH] feat: convertible quantity kinds can now be compared --- src/core/include/mp_units/quantity.h | 6 ++++-- src/core/include/mp_units/quantity_spec.h | 5 ++++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/core/include/mp_units/quantity.h b/src/core/include/mp_units/quantity.h index f33e47b8..9ec4659a 100644 --- a/src/core/include/mp_units/quantity.h +++ b/src/core/include/mp_units/quantity.h @@ -509,7 +509,8 @@ template } template - requires(get_kind(Q1::quantity_spec) == get_kind(Q2::quantity_spec)) && + requires(implicitly_convertible_to(get_kind(Q1::quantity_spec), get_kind(Q2::quantity_spec)) || + implicitly_convertible_to(get_kind(Q2::quantity_spec), get_kind(Q1::quantity_spec))) && std::three_way_comparable_with [[nodiscard]] constexpr auto operator<=>(const Q1& lhs, const Q2& rhs) { @@ -518,7 +519,8 @@ template } template - requires(get_kind(Q1::quantity_spec) == get_kind(Q2::quantity_spec)) && + requires(implicitly_convertible_to(get_kind(Q1::quantity_spec), get_kind(Q2::quantity_spec)) || + implicitly_convertible_to(get_kind(Q2::quantity_spec), get_kind(Q1::quantity_spec))) && std::equality_comparable_with [[nodiscard]] constexpr bool operator==(const Q1& lhs, const Q2& rhs) { diff --git a/src/core/include/mp_units/quantity_spec.h b/src/core/include/mp_units/quantity_spec.h index 81c6a7d6..f29ba8ed 100644 --- a/src/core/include/mp_units/quantity_spec.h +++ b/src/core/include/mp_units/quantity_spec.h @@ -782,12 +782,15 @@ template template [[nodiscard]] consteval QuantitySpec auto common_quantity_spec(Q1 q1, Q2 q2) - requires(get_kind(q1) == get_kind(q2)) + requires(implicitly_convertible_to(get_kind(q1), get_kind(q2)) || + implicitly_convertible_to(get_kind(q2), get_kind(q1))) { if constexpr (q1 == q2) return q1; else if constexpr (detail::have_common_base(q1, q2)) return detail::get_common_base(q1, q2); + else if constexpr (implicitly_convertible_to(get_kind(q1), get_kind(q2))) + return get_kind(q2); else return get_kind(q1); }