From 0dd28c13dde4a183ee95f47b967c1b308eaaa3cf Mon Sep 17 00:00:00 2001 From: Mateusz Pusz Date: Fri, 1 Sep 2023 19:55:50 +0200 Subject: [PATCH] feat: `zero` limited only to comparisons and initialization of quantities --- .../framework_basics/the_affine_space.md | 7 +++-- src/core/include/mp-units/quantity.h | 27 ------------------- src/core/include/mp-units/quantity_point.h | 7 ----- test/unit_test/static/quantity_point_test.cpp | 4 ++- test/unit_test/static/quantity_test.cpp | 17 +++--------- 5 files changed, 10 insertions(+), 52 deletions(-) diff --git a/docs/users_guide/framework_basics/the_affine_space.md b/docs/users_guide/framework_basics/the_affine_space.md index 897c087bd..388282b45 100644 --- a/docs/users_guide/framework_basics/the_affine_space.md +++ b/docs/users_guide/framework_basics/the_affine_space.md @@ -235,10 +235,9 @@ Taxi distance: 31.2544 km !!! note - Subtracting two point origins defined in terms of `absolute_point_origin` - (i.e. `mean_sea_level - mean_sea_level`) results with `zero`. The reason for this is the fact - that those origins do not contain any information about the unit so we are not able to determine - more explicit resulting `quantity` type. + It is not allowed to subtract two point origins defined in terms of `absolute_point_origin` + (i.e. `mean_sea_level - mean_sea_level`) as those do not contain information about the unit + so we are not able to determine a resulting `quantity` type. ### Temperature support diff --git a/src/core/include/mp-units/quantity.h b/src/core/include/mp-units/quantity.h index f1b9851a0..da8c01b19 100644 --- a/src/core/include/mp-units/quantity.h +++ b/src/core/include/mp-units/quantity.h @@ -275,8 +275,6 @@ class quantity { return *this; } - constexpr quantity& operator%=(zero_t) = delete; - template requires(!Quantity) && requires(rep a, const Value b) { { @@ -440,31 +438,6 @@ template return ct(lhs).numerical_value() <=> ct(rhs).numerical_value(); } - -template -[[nodiscard]] constexpr Quantity auto operator+(const quantity& q, zero_t) -{ - return q; -} - -template -[[nodiscard]] constexpr Quantity auto operator+(zero_t, const quantity& q) -{ - return q; -} - -template -[[nodiscard]] constexpr Quantity auto operator-(const quantity& q, zero_t) -{ - return q; -} - -template -[[nodiscard]] constexpr Quantity auto operator-(zero_t, const quantity& q) -{ - return -q; -} - template [[nodiscard]] constexpr bool operator==(const quantity& q, zero_t) { diff --git a/src/core/include/mp-units/quantity_point.h b/src/core/include/mp-units/quantity_point.h index b47035cec..d736d2790 100644 --- a/src/core/include/mp-units/quantity_point.h +++ b/src/core/include/mp-units/quantity_point.h @@ -323,13 +323,6 @@ template QP> return -(qp - po); } -template - requires detail::is_derived_from_specialization_of_absolute_point_origin -[[nodiscard]] constexpr zero_t operator-(PO, PO) -{ - return {}; -} - template PO2> requires QuantitySpecOf, PO2::quantity_spec> && (detail::is_derived_from_specialization_of_relative_point_origin || diff --git a/test/unit_test/static/quantity_point_test.cpp b/test/unit_test/static/quantity_point_test.cpp index 279296391..e93ebf09f 100644 --- a/test/unit_test/static/quantity_point_test.cpp +++ b/test/unit_test/static/quantity_point_test.cpp @@ -736,6 +736,9 @@ concept invalid_binary_operations = requires { // cant'subtract two unrelated points requires !requires { Origin - OtherOrigin; }; + // cant'subtract the same point as we do not know the unit for the resulting quantity + requires !requires { Origin - Origin; }; + // unit constants requires !requires { QP(1) + m; }; requires !requires { QP(1) - m; }; @@ -918,7 +921,6 @@ static_assert(is_of_type>); static_assert(is_of_type>); static_assert(is_of_type>); -static_assert(is_of_type); static_assert(is_of_type<(1 * m + mean_sea_level) - (1 * m + ground_level), quantity>); static_assert(is_of_type<(1 * m + ground_level) - (1 * m + mean_sea_level), quantity>); diff --git a/test/unit_test/static/quantity_test.cpp b/test/unit_test/static/quantity_test.cpp index 1985a9bbd..7baf0f522 100644 --- a/test/unit_test/static/quantity_test.cpp +++ b/test/unit_test/static/quantity_test.cpp @@ -445,11 +445,6 @@ concept invalid_compound_assignments = requires() { requires !requires(Q l) { l *= m; }; requires !requires(Q l) { l /= m; }; requires !requires(Q l) { l %= m; }; - - // zero - requires !requires(Q l) { l *= zero; }; - requires !requires(Q l) { l /= zero; }; - requires !requires(Q l) { l %= zero; }; }; static_assert(invalid_compound_assignments); @@ -481,6 +476,10 @@ concept invalid_binary_operations = requires { requires !requires { m % Q(1); }; // zero + requires !requires(Q a) { a + zero; }; + requires !requires(Q a) { zero + a; }; + requires !requires(Q a) { a - zero; }; + requires !requires(Q a) { zero - a; }; requires !requires(Q a) { a* zero; }; requires !requires(Q a) { zero* a; }; requires !requires(Q a) { zero / a; }; @@ -782,14 +781,6 @@ static_assert(2 * one / (1 * m) == 2 / (1 * m)); // zero static_assert(quantity{zero} == 0 * m); -static_assert((1 * m += zero) == 1 * m); -static_assert((1 * m -= zero) == 1 * m); - -static_assert(1 * m + zero == 1 * m); -static_assert(zero + 1 * m == 1 * m); -static_assert(1 * m - zero == 1 * m); -static_assert(zero - 1 * m == -1 * m); - static_assert(0 * m == zero); static_assert(1 * m != zero); static_assert(-1 * m != zero);