From f4ff02f8e5630ce734fc64f9c23a88e0e278a3e9 Mon Sep 17 00:00:00 2001 From: Mateusz Pusz Date: Wed, 6 Jan 2021 16:44:32 +0100 Subject: [PATCH] fix: quantity's compound operators fixed to behave like `int`s do --- src/include/units/quantity.h | 10 ++++++---- test/unit_test/static/CMakeLists.txt | 2 ++ test/unit_test/static/quantity_test.cpp | 5 +++++ 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/include/units/quantity.h b/src/include/units/quantity.h index ffc05be5..07f89298 100644 --- a/src/include/units/quantity.h +++ b/src/include/units/quantity.h @@ -203,15 +203,17 @@ public: return *this; } - constexpr quantity& operator*=(const rep& rhs) - requires requires(rep a, const rep b) { { a *= b } -> std::same_as; } + template + constexpr quantity& operator*=(const Rep2& rhs) + requires requires(rep a, const Rep2 b) { { a *= b } -> std::same_as; } { value_ *= rhs; return *this; } - constexpr quantity& operator/=(const rep& rhs) - requires requires(rep a, const rep b) { { a /= b } -> std::same_as; } + template + constexpr quantity& operator/=(const Rep2& rhs) + requires requires(rep a, const Rep2 b) { { a /= b } -> std::same_as; } { value_ /= rhs; return *this; diff --git a/test/unit_test/static/CMakeLists.txt b/test/unit_test/static/CMakeLists.txt index d44aa529..804544f4 100644 --- a/test/unit_test/static/CMakeLists.txt +++ b/test/unit_test/static/CMakeLists.txt @@ -51,3 +51,5 @@ target_link_libraries(unit_tests_static PRIVATE mp::units ) + +set_source_files_properties(quantity_test.cpp PROPERTIES COMPILE_FLAGS -Wno-conversion) diff --git a/test/unit_test/static/quantity_test.cpp b/test/unit_test/static/quantity_test.cpp index 5cd3c2f8..76d87e08 100644 --- a/test/unit_test/static/quantity_test.cpp +++ b/test/unit_test/static/quantity_test.cpp @@ -283,6 +283,11 @@ static_assert((2.5_q_m *= 3).count() == 7.5); static_assert((7.5_q_m /= 3).count() == 2.5); static_assert((3500_q_m %= 1_q_km).count() == 500); +// next two lines trigger the gcc 'Wconversion' warning +// (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); + template concept invalid_compound_assignments = requires() { // truncating not allowed