From f1c43e9c5a92812ff2f9b53854f7e4064ea15a81 Mon Sep 17 00:00:00 2001 From: Giel van Schijndel Date: Thu, 8 Sep 2016 16:27:31 +0200 Subject: [PATCH 1/2] Add test proving that comparing heterogenous comparisons fail More specifically: comparing quantities of heterogenous and homogenous unit systems fails to build. --- test/Jamfile.v2 | 1 + test/test_heterogenous_comparison.cpp | 38 +++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 test/test_heterogenous_comparison.cpp diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index 16b9ac64..22ca292a 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -27,6 +27,7 @@ project boost/units/test : alias test_framework : /boost//unit_test_framework/off ; +compile-fail test_heterogenous_comparison.cpp ; compile test_predicates.cpp ; compile test_negative_denominator.cpp ; compile test_dimensionless_ice1.cpp ; diff --git a/test/test_heterogenous_comparison.cpp b/test/test_heterogenous_comparison.cpp new file mode 100644 index 00000000..a2d003a2 --- /dev/null +++ b/test/test_heterogenous_comparison.cpp @@ -0,0 +1,38 @@ +// Boost.Units - A C++ library for zero-overhead dimensional analysis and +// unit/quantity manipulation and conversion +// +// Copyright (C) 2016 Giel van Schijndel +// +// Distributed under the Boost Software License, Version 1.0. (See +// accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +/** +\file + +\brief test_heterogenous_comparison.cpp + +\details +Verify that comparing a quantity from a heterogenous system with a +quantity from a homogenous system works. + +Output: +@verbatim +@endverbatim +**/ + +#include +#include + +namespace bu = boost::units; + +int main(int,char *[]) +{ + bu::quantity x; + int y = 0; + + x < y * bu::si::meter; + + return 0; +} + From 33829c05f08695cff3cadb4acc9425b3d5398bb0 Mon Sep 17 00:00:00 2001 From: Giel van Schijndel Date: Thu, 8 Sep 2016 16:11:53 +0200 Subject: [PATCH 2/2] Make comparisons between heterogenous and homogenous quantities possible --- include/boost/units/quantity.hpp | 84 ++++++++++++++++++++------------ test/Jamfile.v2 | 2 +- 2 files changed, 55 insertions(+), 31 deletions(-) diff --git a/include/boost/units/quantity.hpp b/include/boost/units/quantity.hpp index 7bdb78ef..8f087177 100644 --- a/include/boost/units/quantity.hpp +++ b/include/boost/units/quantity.hpp @@ -1176,75 +1176,99 @@ operator/(const quantity& lhs, } /// runtime operator== -template inline -bool -operator==(const quantity& val1, - const quantity& val2) +typename boost::enable_if< + typename is_implicitly_convertible::type, + bool +>::type +operator==(const quantity& val1, + const quantity& val2) { - return val1.value() == val2.value(); + return val1.value() == quantity(val2).value(); } /// runtime operator!= -template inline -bool -operator!=(const quantity& val1, - const quantity& val2) +typename boost::enable_if< + typename is_implicitly_convertible::type, + bool +>::type +operator!=(const quantity& val1, + const quantity& val2) { - return val1.value() != val2.value(); + return val1.value() != quantity(val2).value(); } /// runtime operator< -template inline -bool -operator<(const quantity& val1, - const quantity& val2) +typename boost::enable_if< + typename is_implicitly_convertible::type, + bool +>::type +operator<(const quantity& val1, + const quantity& val2) { - return val1.value() < val2.value(); + return val1.value() < quantity(val2).value(); } /// runtime operator<= -template inline -bool -operator<=(const quantity& val1, - const quantity& val2) +typename boost::enable_if< + typename is_implicitly_convertible::type, + bool +>::type +operator<=(const quantity& val1, + const quantity& val2) { - return val1.value() <= val2.value(); + return val1.value() <= quantity(val2).value(); } /// runtime operator> -template inline -bool -operator>(const quantity& val1, - const quantity& val2) +typename boost::enable_if< + typename is_implicitly_convertible::type, + bool +>::type +operator>(const quantity& val1, + const quantity& val2) { - return val1.value() > val2.value(); + return val1.value() > quantity(val2).value(); } /// runtime operator>= -template inline -bool -operator>=(const quantity& val1, - const quantity& val2) +typename boost::enable_if< + typename is_implicitly_convertible::type, + bool +>::type +operator>=(const quantity& val1, + const quantity& val2) { - return val1.value() >= val2.value(); + return val1.value() >= quantity(val2).value(); } } // namespace units diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index 22ca292a..99b84a91 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -27,7 +27,7 @@ project boost/units/test : alias test_framework : /boost//unit_test_framework/off ; -compile-fail test_heterogenous_comparison.cpp ; +compile test_heterogenous_comparison.cpp ; compile test_predicates.cpp ; compile test_negative_denominator.cpp ; compile test_dimensionless_ice1.cpp ;