From d195e2b3bfec59bcafb7dd4c547480da4585f5d8 Mon Sep 17 00:00:00 2001 From: Mateusz Pusz Date: Fri, 3 Nov 2023 21:18:57 +0100 Subject: [PATCH] fix: equality for dimensions fixed to provide transitivity --- src/core/include/mp-units/dimension.h | 19 ++++++++++++++++--- test/unit_test/static/dimension_test.cpp | 6 ++++-- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/core/include/mp-units/dimension.h b/src/core/include/mp-units/dimension.h index f04d7363..14ce1898 100644 --- a/src/core/include/mp-units/dimension.h +++ b/src/core/include/mp-units/dimension.h @@ -150,10 +150,23 @@ template Rhs{}); } -template -[[nodiscard]] consteval bool operator==(Lhs, Rhs) +namespace detail { + +template +[[nodiscard]] consteval std::true_type derived_from_the_same_base_dimension(const base_dimension&, + const base_dimension&) { - return std::derived_from || std::derived_from; + return {}; +} + +[[nodiscard]] consteval std::false_type derived_from_the_same_base_dimension(...) { return {}; } + +} // namespace detail + +template +[[nodiscard]] consteval bool operator==(Lhs lhs, Rhs rhs) +{ + return is_same_v || detail::derived_from_the_same_base_dimension(lhs, rhs); } [[nodiscard]] consteval Dimension auto inverse(Dimension auto d) { return dimension_one / d; } diff --git a/test/unit_test/static/dimension_test.cpp b/test/unit_test/static/dimension_test.cpp index 08eacb47..808620d7 100644 --- a/test/unit_test/static/dimension_test.cpp +++ b/test/unit_test/static/dimension_test.cpp @@ -38,7 +38,8 @@ inline constexpr struct length_ : base_dimension<"L"> {} length; inline constexpr struct mass_ : base_dimension<"M"> {} mass; inline constexpr struct time_ : base_dimension<"T"> {} time; -inline constexpr struct my_length_ : decltype(length) {} my_length; +inline constexpr struct my_length1_ : decltype(length) {} my_length1; +inline constexpr struct my_length2_ : decltype(length) {} my_length2; QUANTITY_SPEC_(q_time, time); inline constexpr struct second_ : named_unit<"s", kind_of> {} second; @@ -170,7 +171,8 @@ static_assert(invalid_operations