From b119abed56f96628bbd803f28c27610fb8056db6 Mon Sep 17 00:00:00 2001 From: Yves Delley Date: Fri, 10 May 2024 20:01:35 +0200 Subject: [PATCH 1/5] add test for lvalue references in quantity_cast --- test/static/quantity_test.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/test/static/quantity_test.cpp b/test/static/quantity_test.cpp index 4931ce74..30a74282 100644 --- a/test/static/quantity_test.cpp +++ b/test/static/quantity_test.cpp @@ -937,7 +937,10 @@ static_assert(is_of_type(1 * m), quantity(isq::length(1 * m)), quantity>); static_assert(is_of_type>(isq::length(1 * m)), quantity>); static_assert(is_of_type>(isq::distance(1 * m)), quantity>); - +// lvalue references in quantity_cast +inline constexpr quantity to_distance(quantity arg){ + return quantity_cast(arg); +} // QuantityOf static_assert(QuantityOf, isq::length>); From d8371074f85fd09bac052a0e71bce00b68b1d3cb Mon Sep 17 00:00:00 2001 From: Yves Delley Date: Fri, 10 May 2024 20:07:07 +0200 Subject: [PATCH 2/5] fix quantity_cast to accept lvalue references --- src/core/include/mp-units/framework/quantity_cast.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/core/include/mp-units/framework/quantity_cast.h b/src/core/include/mp-units/framework/quantity_cast.h index 64d0154b..38e8aaf7 100644 --- a/src/core/include/mp-units/framework/quantity_cast.h +++ b/src/core/include/mp-units/framework/quantity_cast.h @@ -53,10 +53,10 @@ namespace mp_units { * @tparam ToQS a quantity specification to use for a target quantity */ template - requires Quantity> && (castable(Q::quantity_spec, ToQS)) + requires Quantity> && (castable(std::remove_reference_t::quantity_spec, ToQS)) [[nodiscard]] constexpr Quantity auto quantity_cast(Q&& q) { - return quantity{std::forward(q).numerical_value_is_an_implementation_detail_, make_reference(ToQS, Q::unit)}; + return quantity{std::forward(q).numerical_value_is_an_implementation_detail_, make_reference(ToQS, q.unit)}; } /** @@ -77,7 +77,7 @@ template * @tparam ToQS a quantity specification to use for a target quantity point */ template - requires QuantityPoint> && (castable(QP::quantity_spec, ToQS)) + requires QuantityPoint> && (castable(std::remove_reference_t::quantity_spec, ToQS)) [[nodiscard]] constexpr QuantityPoint auto quantity_cast(QP&& qp) { return QP{quantity_cast(std::forward(qp).quantity_from_origin_is_an_implementation_detail_), From d03f9c17a340532536a5207ecb4f41226716eef0 Mon Sep 17 00:00:00 2001 From: Yves Delley Date: Fri, 10 May 2024 20:15:15 +0200 Subject: [PATCH 3/5] fix constexpr and format --- src/core/include/mp-units/framework/quantity_cast.h | 5 +++-- test/static/quantity_test.cpp | 3 ++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/core/include/mp-units/framework/quantity_cast.h b/src/core/include/mp-units/framework/quantity_cast.h index 38e8aaf7..53b14815 100644 --- a/src/core/include/mp-units/framework/quantity_cast.h +++ b/src/core/include/mp-units/framework/quantity_cast.h @@ -56,7 +56,8 @@ template requires Quantity> && (castable(std::remove_reference_t::quantity_spec, ToQS)) [[nodiscard]] constexpr Quantity auto quantity_cast(Q&& q) { - return quantity{std::forward(q).numerical_value_is_an_implementation_detail_, make_reference(ToQS, q.unit)}; + return quantity{std::forward(q).numerical_value_is_an_implementation_detail_, + make_reference(ToQS, std::remove_reference_t::unit)}; } /** @@ -81,7 +82,7 @@ template [[nodiscard]] constexpr QuantityPoint auto quantity_cast(QP&& qp) { return QP{quantity_cast(std::forward(qp).quantity_from_origin_is_an_implementation_detail_), - qp.point_origin}; + std::remove_reference_t::point_origin}; } } // namespace mp_units diff --git a/test/static/quantity_test.cpp b/test/static/quantity_test.cpp index 30a74282..b8111368 100644 --- a/test/static/quantity_test.cpp +++ b/test/static/quantity_test.cpp @@ -938,7 +938,8 @@ static_assert(is_of_type(isq::length(1 * m)), quant static_assert(is_of_type>(isq::length(1 * m)), quantity>); static_assert(is_of_type>(isq::distance(1 * m)), quantity>); // lvalue references in quantity_cast -inline constexpr quantity to_distance(quantity arg){ +inline constexpr quantity to_distance(quantity arg) +{ return quantity_cast(arg); } From c2fa8a9000a612e7046251a7ba94927ef4b37a24 Mon Sep 17 00:00:00 2001 From: Yves Delley Date: Fri, 10 May 2024 21:13:21 +0200 Subject: [PATCH 4/5] avoid using a function for the lvalue test --- test/static/quantity_test.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/static/quantity_test.cpp b/test/static/quantity_test.cpp index b8111368..ea375e38 100644 --- a/test/static/quantity_test.cpp +++ b/test/static/quantity_test.cpp @@ -938,10 +938,10 @@ static_assert(is_of_type(isq::length(1 * m)), quant static_assert(is_of_type>(isq::length(1 * m)), quantity>); static_assert(is_of_type>(isq::distance(1 * m)), quantity>); // lvalue references in quantity_cast -inline constexpr quantity to_distance(quantity arg) -{ - return quantity_cast(arg); -} +namespace lvalue_tests { +quantity lvalue_q = 1 * m; +static_assert(is_of_type(lvalue_q), quantity>); +} // namespace lvalue_tests // QuantityOf static_assert(QuantityOf, isq::length>); From 09c7b059c7700fb34c5f5277c2fd73923675a7e7 Mon Sep 17 00:00:00 2001 From: Yves Delley Date: Fri, 10 May 2024 21:38:40 +0200 Subject: [PATCH 5/5] review suggestions Co-authored-by: Mateusz Pusz --- test/static/quantity_test.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/static/quantity_test.cpp b/test/static/quantity_test.cpp index ea375e38..69b653f7 100644 --- a/test/static/quantity_test.cpp +++ b/test/static/quantity_test.cpp @@ -939,7 +939,7 @@ static_assert(is_of_type>(isq::length(1 * m)) static_assert(is_of_type>(isq::distance(1 * m)), quantity>); // lvalue references in quantity_cast namespace lvalue_tests { -quantity lvalue_q = 1 * m; +constexpr quantity lvalue_q = 1 * m; static_assert(is_of_type(lvalue_q), quantity>); } // namespace lvalue_tests