From 49da1ced3a2144c7850442ae28974b60c943b124 Mon Sep 17 00:00:00 2001 From: Mateusz Pusz Date: Wed, 20 Sep 2023 20:31:49 +0200 Subject: [PATCH] refactor: `make_reference` added and used in constraints for `quantity` and `quantity_point` `quantity_spec[unit]` syntax will not work for natural units and we want the interface to be widely applicable to all domains. --- src/core/include/mp-units/quantity.h | 8 +++---- src/core/include/mp-units/quantity_point.h | 6 +++--- src/core/include/mp-units/quantity_spec.h | 25 +++++++++++----------- 3 files changed, 20 insertions(+), 19 deletions(-) diff --git a/src/core/include/mp-units/quantity.h b/src/core/include/mp-units/quantity.h index c8d1d3196..e1e989cd5 100644 --- a/src/core/include/mp-units/quantity.h +++ b/src/core/include/mp-units/quantity.h @@ -143,15 +143,15 @@ class quantity { // conversions template - requires detail::QuantityConvertibleTo> - [[nodiscard]] constexpr quantity in(U) const + requires detail::QuantityConvertibleTo> + [[nodiscard]] constexpr quantity in(U) const { - return quantity{*this}; + return quantity{*this}; } template requires requires(quantity q) { value_cast(q); } - [[nodiscard]] constexpr quantity force_in(U) const + [[nodiscard]] constexpr quantity force_in(U) const { return value_cast(*this); } diff --git a/src/core/include/mp-units/quantity_point.h b/src/core/include/mp-units/quantity_point.h index 910620233..3a8dd8bd1 100644 --- a/src/core/include/mp-units/quantity_point.h +++ b/src/core/include/mp-units/quantity_point.h @@ -169,15 +169,15 @@ class quantity_point { } template - requires detail::QuantityConvertibleTo> - [[nodiscard]] constexpr quantity_point in(U) const + requires detail::QuantityConvertibleTo> + [[nodiscard]] constexpr quantity_point in(U) const { return make_quantity_point(quantity_ref_from(PO).in(U{})); } template requires requires(quantity_type q) { value_cast(q); } - [[nodiscard]] constexpr quantity_point force_in(U) const + [[nodiscard]] constexpr quantity_point force_in(U) const { return make_quantity_point(quantity_ref_from(PO).force_in(U{})); } diff --git a/src/core/include/mp-units/quantity_spec.h b/src/core/include/mp-units/quantity_spec.h index b6e2e2a1a..1a55f50cc 100644 --- a/src/core/include/mp-units/quantity_spec.h +++ b/src/core/include/mp-units/quantity_spec.h @@ -38,6 +38,16 @@ namespace mp_units { namespace detail { + +template +[[nodiscard]] consteval Reference auto make_reference(QS qs, U u) +{ + if constexpr (detail::QuantityKindSpec) + return u; + else + return reference{}; +} + // TODO revise the note in the below comment /** * @brief Returns the most restrictive character from the list @@ -100,10 +110,7 @@ struct quantity_spec_interface { template U> [[nodiscard]] consteval Reference auto operator[](this Self self, U u) { - if constexpr (detail::QuantityKindSpec) - return u; - else - return reference{}; + return detail::make_reference(self, u); } template @@ -117,10 +124,7 @@ struct quantity_spec_interface { template U> [[nodiscard]] MP_UNITS_CONSTEVAL Reference auto operator[](U u) const { - if constexpr (detail::QuantityKindSpec) - return u; - else - return reference{}; + return detail::make_reference(Self{}, u); } template @@ -296,10 +300,7 @@ struct quantity_spec : std::remove_const_t { template U> [[nodiscard]] MP_UNITS_CONSTEVAL Reference auto operator[](U u) const { - if constexpr (detail::QuantityKindSpec) - return u; - else - return reference{}; + return detail::make_reference(Self{}, u); } template