Avoid ambiguous calls to abs with unsigned integers.

This commit is contained in:
Atgeirr Flø Rasmussen
2016-06-15 10:55:42 +02:00
parent 33b29e9991
commit 06ebdc9268
2 changed files with 28 additions and 11 deletions

View File

@@ -578,18 +578,32 @@ private:
namespace detail namespace detail
{ {
/// \brief Computes the maximum of theabsolute values of two values. /// \brief Computes the maximum of the absolute values of two values.
template<typename T> template<typename T>
struct MaxAbsFunctor struct MaxAbsFunctor
{
typedef T result_type;
result_type operator()(const T& t1, const T& t2)
{ {
return std::max(std::abs(t1),std::abs(t2)); typedef T result_type;
}
}; result_type operator()(const T& t1, const T& t2)
{
return std::max(std::abs(t1),std::abs(t2));
}
};
/// \brief Specialization to avoid ambiguous abs() for unsigned integers.
template <>
struct MaxAbsFunctor<std::size_t>
{
typedef std::size_t result_type;
result_type operator()(const std::size_t& t1, const std::size_t& t2)
{
return std::max(t1, t2);
}
};
} }
/// \brief Create a functor for computing a global L infinity norm /// \brief Create a functor for computing a global L infinity norm
/// ///
/// To be used with ParallelISTLInformation::computeReduction. /// To be used with ParallelISTLInformation::computeReduction.

View File

@@ -33,6 +33,7 @@
#include <functional> #include <functional>
#ifdef HAVE_DUNE_ISTL #ifdef HAVE_DUNE_ISTL
template<typename T> template<typename T>
void runSumMaxMinTest(const T offset) void runSumMaxMinTest(const T offset)
{ {
@@ -60,7 +61,9 @@ void runSumMaxMinTest(const T offset)
BOOST_CHECK(std::get<1>(values)==std::max(N+offset-1, std::get<1>(oldvalues))); BOOST_CHECK(std::get<1>(values)==std::max(N+offset-1, std::get<1>(oldvalues)));
BOOST_CHECK(std::get<2>(values)==std::min(offset, std::get<2>(oldvalues))); BOOST_CHECK(std::get<2>(values)==std::min(offset, std::get<2>(oldvalues)));
BOOST_CHECK(std::get<3>(values)==((end-1)*end*(2*end-1)-(start-1)*start*(2*start-1))/6+std::get<3>(oldvalues)); BOOST_CHECK(std::get<3>(values)==((end-1)*end*(2*end-1)-(start-1)*start*(2*start-1))/6+std::get<3>(oldvalues));
BOOST_CHECK(std::get<4>(values)==std::max(std::abs(offset),std::abs(N+offset-1))); // Must avoid std::abs() directly to prevent ambiguity with unsigned integers.
Opm::Reduction::detail::MaxAbsFunctor<T> maxabsfunc;
BOOST_CHECK(std::get<4>(values)==maxabsfunc(offset, N+offset-1));
} }
BOOST_AUTO_TEST_CASE(tupleReductionTestInt) BOOST_AUTO_TEST_CASE(tupleReductionTestInt)