From b76337d5bb181c41f3a4cfb51915379587887f68 Mon Sep 17 00:00:00 2001 From: Mateusz Pusz Date: Thu, 14 Sep 2023 12:42:45 +0200 Subject: [PATCH] feat: pre-increment and pre-decrement operators now preserve value category --- src/core/include/mp-units/quantity.h | 18 ++++++++++-------- src/core/include/mp-units/quantity_point.h | 18 ++++++++++-------- 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/src/core/include/mp-units/quantity.h b/src/core/include/mp-units/quantity.h index 2e9fab49..c8d1d319 100644 --- a/src/core/include/mp-units/quantity.h +++ b/src/core/include/mp-units/quantity.h @@ -210,15 +210,16 @@ public: return make_quantity(-numerical_value_); } - constexpr quantity& operator++() - requires requires(rep v) { + template + friend constexpr decltype(auto) operator++(Q&& q) + requires std::derived_from, quantity> && requires(rep v) { { ++v } -> std::same_as; } { - ++numerical_value_; - return *this; + ++q.numerical_value_; + return std::forward(q); } [[nodiscard]] constexpr Quantity auto operator++(int) @@ -231,15 +232,16 @@ public: return make_quantity(numerical_value_++); } - constexpr quantity& operator--() - requires requires(rep v) { + template + friend constexpr decltype(auto) operator--(Q&& q) + requires std::derived_from, quantity> && requires(rep v) { { --v } -> std::same_as; } { - --numerical_value_; - return *this; + --q.numerical_value_; + return std::forward(q); } [[nodiscard]] constexpr Quantity auto operator--(int) diff --git a/src/core/include/mp-units/quantity_point.h b/src/core/include/mp-units/quantity_point.h index bcf48acb..91062023 100644 --- a/src/core/include/mp-units/quantity_point.h +++ b/src/core/include/mp-units/quantity_point.h @@ -183,11 +183,12 @@ public: } // member unary operators - constexpr quantity_point& operator++() - requires requires { ++quantity_from_origin_; } + template + friend constexpr decltype(auto) operator++(QP&& qp) + requires std::derived_from, quantity_point> && requires { ++qp.quantity_from_origin_; } { - ++quantity_from_origin_; - return *this; + ++qp.quantity_from_origin_; + return std::forward(qp); } [[nodiscard]] constexpr quantity_point operator++(int) @@ -196,11 +197,12 @@ public: return quantity_point(quantity_from_origin_++); } - constexpr quantity_point& operator--() - requires requires { --quantity_from_origin_; } + template + friend constexpr decltype(auto) operator--(QP&& qp) + requires std::derived_from, quantity_point> && requires { --qp.quantity_from_origin_; } { - --quantity_from_origin_; - return *this; + --qp.quantity_from_origin_; + return std::forward(qp); } [[nodiscard]] constexpr quantity_point operator--(int)