Merge pull request #1039 from atgeirr/fix-compile-maxabs-unsigned

Avoid ambiguous calls to abs with unsigned integers.
This commit is contained in:
Atgeirr Flø Rasmussen 2016-06-17 08:02:20 +02:00 committed by GitHub
commit d9c33cbcb6
2 changed files with 29 additions and 11 deletions

View File

@ -589,18 +589,33 @@ private:
namespace detail
{
/// \brief Computes the maximum of theabsolute values of two values.
template<typename T>
struct MaxAbsFunctor
{
typedef T result_type;
result_type operator()(const T& t1, const T& t2)
/// \brief Computes the maximum of the absolute values of two values.
template<typename T, typename Enable = void>
struct MaxAbsFunctor
{
return std::max(std::abs(t1),std::abs(t2));
}
};
using result_type = T;
result_type operator()(const T& t1,
const T& t2)
{
return std::max(std::abs(t1), std::abs(t2));
}
};
// Specialization for unsigned integers. They need their own
// version since abs(x) is ambiguous (as well as somewhat
// meaningless).
template<typename T>
struct MaxAbsFunctor<T, typename std::enable_if<std::is_unsigned<T>::value>::type>
{
using result_type = T;
result_type operator()(const T& t1,
const T& t2)
{
return std::max(t1, t2);
}
};
}
/// \brief Create a functor for computing a global L infinity norm
///
/// To be used with ParallelISTLInformation::computeReduction.

View File

@ -33,6 +33,7 @@
#include <functional>
#ifdef HAVE_DUNE_ISTL
template<typename T>
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<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<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)