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 010480a7de
2 changed files with 29 additions and 11 deletions

View File

@ -590,17 +590,32 @@ private:
namespace detail namespace detail
{ {
/// \brief Computes the maximum of the absolute values of two values. /// \brief Computes the maximum of the absolute values of two values.
template<typename T> template<typename T, typename Enable = void>
struct MaxAbsFunctor struct MaxAbsFunctor
{ {
typedef T result_type; using result_type = T;
result_type operator()(const T& t1,
result_type operator()(const T& t1, const T& t2) const T& t2)
{ {
return std::max(std::abs(t1), std::abs(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 /// \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)