Skip to content

Make comparisons between heterogenous and homogenous quantities possible #24

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 54 additions & 30 deletions include/boost/units/quantity.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1176,75 +1176,99 @@ operator/(const quantity<Unit1,X>& lhs,
}

/// runtime operator==
template<class Unit,
template<class Unit1,
class Unit2,
class X,
class Y>
inline
bool
operator==(const quantity<Unit,X>& val1,
const quantity<Unit,Y>& val2)
typename boost::enable_if<
typename is_implicitly_convertible<Unit1, Unit2>::type,
bool
>::type
operator==(const quantity<Unit1,X>& val1,
const quantity<Unit2,Y>& val2)
{
return val1.value() == val2.value();
return val1.value() == quantity<Unit1, Y>(val2).value();
}

/// runtime operator!=
template<class Unit,
template<class Unit1,
class Unit2,
class X,
class Y>
inline
bool
operator!=(const quantity<Unit,X>& val1,
const quantity<Unit,Y>& val2)
typename boost::enable_if<
typename is_implicitly_convertible<Unit1, Unit2>::type,
bool
>::type
operator!=(const quantity<Unit1,X>& val1,
const quantity<Unit2,Y>& val2)
{
return val1.value() != val2.value();
return val1.value() != quantity<Unit1, Y>(val2).value();
}

/// runtime operator<
template<class Unit,
template<class Unit1,
class Unit2,
class X,
class Y>
inline
bool
operator<(const quantity<Unit,X>& val1,
const quantity<Unit,Y>& val2)
typename boost::enable_if<
typename is_implicitly_convertible<Unit1, Unit2>::type,
bool
>::type
operator<(const quantity<Unit1,X>& val1,
const quantity<Unit2,Y>& val2)
{
return val1.value() < val2.value();
return val1.value() < quantity<Unit1, Y>(val2).value();
}

/// runtime operator<=
template<class Unit,
template<class Unit1,
class Unit2,
class X,
class Y>
inline
bool
operator<=(const quantity<Unit,X>& val1,
const quantity<Unit,Y>& val2)
typename boost::enable_if<
typename is_implicitly_convertible<Unit1, Unit2>::type,
bool
>::type
operator<=(const quantity<Unit1,X>& val1,
const quantity<Unit2,Y>& val2)
{
return val1.value() <= val2.value();
return val1.value() <= quantity<Unit1, Y>(val2).value();
}

/// runtime operator>
template<class Unit,
template<class Unit1,
class Unit2,
class X,
class Y>
inline
bool
operator>(const quantity<Unit,X>& val1,
const quantity<Unit,Y>& val2)
typename boost::enable_if<
typename is_implicitly_convertible<Unit1, Unit2>::type,
bool
>::type
operator>(const quantity<Unit1,X>& val1,
const quantity<Unit2,Y>& val2)
{
return val1.value() > val2.value();
return val1.value() > quantity<Unit1, Y>(val2).value();
}

/// runtime operator>=
template<class Unit,
template<class Unit1,
class Unit2,
class X,
class Y>
inline
bool
operator>=(const quantity<Unit,X>& val1,
const quantity<Unit,Y>& val2)
typename boost::enable_if<
typename is_implicitly_convertible<Unit1, Unit2>::type,
bool
>::type
operator>=(const quantity<Unit1,X>& val1,
const quantity<Unit2,Y>& val2)
{
return val1.value() >= val2.value();
return val1.value() >= quantity<Unit1, Y>(val2).value();
}

} // namespace units
Expand Down
1 change: 1 addition & 0 deletions test/Jamfile.v2
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ project boost/units/test :

alias test_framework : /boost//unit_test_framework/<warnings-as-errors>off ;

compile test_heterogenous_comparison.cpp ;
compile test_predicates.cpp ;
compile test_negative_denominator.cpp ;
compile test_dimensionless_ice1.cpp ;
Expand Down
38 changes: 38 additions & 0 deletions test/test_heterogenous_comparison.cpp
Original file line number Diff line number Diff line change
@@ -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 <boost/units/quantity.hpp>
#include <boost/units/systems/si/length.hpp>

namespace bu = boost::units;

int main(int,char *[])
{
bu::quantity<bu::si::meter_base_unit::unit_type> x;
int y = 0;

x < y * bu::si::meter;

return 0;
}