From a168f49b667050bdee65202d73dd05c81036107d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johel=20Ernesto=20Guerrero=20Pe=C3=B1a?= Date: Sat, 6 Feb 2021 21:42:25 -0400 Subject: [PATCH] fix: lack of compound assignment from dimensionless --- src/include/units/quantity.h | 14 ++++++++++++++ test/unit_test/static/quantity_test.cpp | 8 ++++++++ 2 files changed, 22 insertions(+) diff --git a/src/include/units/quantity.h b/src/include/units/quantity.h index 8911cc73..574318a7 100644 --- a/src/include/units/quantity.h +++ b/src/include/units/quantity.h @@ -210,6 +210,13 @@ public: value_ *= rhs; return *this; } + template + constexpr quantity& operator*=(const dimensionless& rhs) + requires requires(rep a, const Rep2 b) { { a *= b } -> std::same_as; } + { + value_ *= rhs.count(); + return *this; + } template constexpr quantity& operator/=(const Rep2& rhs) @@ -218,6 +225,13 @@ public: value_ /= rhs; return *this; } + template + constexpr quantity& operator/=(const dimensionless& rhs) + requires requires(rep a, const Rep2 b) { { a /= b } -> std::same_as; } + { + value_ /= rhs.count(); + return *this; + } constexpr quantity& operator%=(const rep& rhs) requires (!floating_point_) && diff --git a/test/unit_test/static/quantity_test.cpp b/test/unit_test/static/quantity_test.cpp index be52bc9a..81512094 100644 --- a/test/unit_test/static/quantity_test.cpp +++ b/test/unit_test/static/quantity_test.cpp @@ -275,6 +275,8 @@ static_assert((1_q_m += 1_q_m).count() == 2); static_assert((2_q_m -= 1_q_m).count() == 1); static_assert((1_q_m *= 2).count() == 2); static_assert((2_q_m /= 2).count() == 1); +static_assert((1_q_m *= quantity(2)).count() == 2); +static_assert((2_q_m /= quantity(2)).count() == 1); static_assert((7_q_m %= 2).count() == 1); static_assert((7_q_m %= 2_q_m).count() == 1); @@ -285,6 +287,8 @@ static_assert((5.5_q_m -= 3_q_m).count() == 2.5); static_assert((1123_q_m -= 1_q_km).count() == 123); static_assert((2.5_q_m *= 3).count() == 7.5); static_assert((7.5_q_m /= 3).count() == 2.5); +static_assert((2.5_q_m *= quantity(3)).count() == 7.5); +static_assert((7.5_q_m /= quantity(3)).count() == 2.5); static_assert((3500_q_m %= 1_q_km).count() == 500); #ifndef COMP_MSVC // TODO ICE (https://developercommunity2.visualstudio.com/t/ICE-on-a-constexpr-operator-in-mp-unit/1302907) @@ -292,6 +296,8 @@ static_assert((3500_q_m %= 1_q_km).count() == 500); // (warning disabled in CMake for this file) static_assert((22_q_m *= 33.33).count() == 733); static_assert((22_q_m /= 3.33).count() == 6); +static_assert((22_q_m *= quantity(33.33)).count() == 733); +static_assert((22_q_m /= quantity(3.33)).count() == 6); #endif template @@ -512,6 +518,8 @@ static_assert((10_q_s * 2_q_kHz).count() == 20); // dimensionless +static_assert((quantity{3} *= quantity{2}) == 6); +static_assert((quantity{6} /= quantity{2}) == 3); static_assert(quantity{1} + quantity{1} == 2); static_assert(1 + quantity{1} == 2); static_assert(quantity{1} + 1 == 2);