fix: quantity's compound operators fixed to behave like ints do

This commit is contained in:
Mateusz Pusz
2021-01-06 16:44:32 +01:00
parent a84c9addfa
commit f4ff02f8e5
3 changed files with 13 additions and 4 deletions

View File

@@ -203,15 +203,17 @@ public:
return *this; return *this;
} }
constexpr quantity& operator*=(const rep& rhs) template<typename Rep2>
requires requires(rep a, const rep b) { { a *= b } -> std::same_as<rep&>; } constexpr quantity& operator*=(const Rep2& rhs)
requires requires(rep a, const Rep2 b) { { a *= b } -> std::same_as<rep&>; }
{ {
value_ *= rhs; value_ *= rhs;
return *this; return *this;
} }
constexpr quantity& operator/=(const rep& rhs) template<typename Rep2>
requires requires(rep a, const rep b) { { a /= b } -> std::same_as<rep&>; } constexpr quantity& operator/=(const Rep2& rhs)
requires requires(rep a, const Rep2 b) { { a /= b } -> std::same_as<rep&>; }
{ {
value_ /= rhs; value_ /= rhs;
return *this; return *this;

View File

@@ -51,3 +51,5 @@ target_link_libraries(unit_tests_static
PRIVATE PRIVATE
mp::units mp::units
) )
set_source_files_properties(quantity_test.cpp PROPERTIES COMPILE_FLAGS -Wno-conversion)

View File

@@ -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((7.5_q_m /= 3).count() == 2.5);
static_assert((3500_q_m %= 1_q_km).count() == 500); 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<typename Metre> template<typename Metre>
concept invalid_compound_assignments = requires() { concept invalid_compound_assignments = requires() {
// truncating not allowed // truncating not allowed