Commit a1e3a62d authored by Jakub Klinkovský's avatar Jakub Klinkovský
Browse files

Added static_asserts to the getIdempotent methods in Functional.h

Also fixed the idempotent values for Max and MaxWithArg
(std::numerical_limits<T>::lowest() vs std::numerical_limits<T>::min())
parent 090a8f29
Loading
Loading
Loading
Loading
+35 −10
Original line number Diff line number Diff line
@@ -23,7 +23,7 @@ namespace TNL {
struct Plus : public std::plus< void >
{
   template< typename T >
   static constexpr T getIdempotent() { return 0; };
   static constexpr T getIdempotent() { return 0; }
};

/**
@@ -32,7 +32,7 @@ struct Plus : public std::plus< void >
struct Multiplies : public std::multiplies< void >
{
   template< typename T >
   static constexpr T getIdempotent() { return 1; };
   static constexpr T getIdempotent() { return 1; }
};

/**
@@ -41,7 +41,12 @@ struct Multiplies : public std::multiplies< void >
struct Min
{
   template< typename T >
   static constexpr T getIdempotent() { return std::numeric_limits< T >::max(); };
   static constexpr T getIdempotent()
   {
      static_assert( std::numeric_limits< T >::is_specialized,
                     "std::numeric_limits is not specialized for the requested type" );
      return std::numeric_limits< T >::max();
   }

   template< typename Value >
   constexpr Value operator()( const Value& lhs, const Value& rhs ) const
@@ -58,7 +63,12 @@ struct Min
struct Max
{
   template< typename T >
   static constexpr T getIdempotent() { return std::numeric_limits< T >::min(); };
   static constexpr T getIdempotent()
   {
      static_assert( std::numeric_limits< T >::is_specialized,
                     "std::numeric_limits is not specialized for the requested type" );
      return std::numeric_limits< T >::lowest();
   }

   template< typename Value >
   constexpr Value operator()( const Value& lhs, const Value& rhs ) const
@@ -75,7 +85,12 @@ struct Max
struct MinWithArg
{
   template< typename T >
   static constexpr T getIdempotent() { return std::numeric_limits< T >::max(); };
   static constexpr T getIdempotent()
   {
      static_assert( std::numeric_limits< T >::is_specialized,
                     "std::numeric_limits is not specialized for the requested type" );
      return std::numeric_limits< T >::max();
   }

   template< typename Value, typename Index >
   constexpr void operator()( Value& lhs, const Value& rhs, Index& lhsIdx, const Index& rhsIdx ) const
@@ -98,7 +113,12 @@ struct MinWithArg
struct MaxWithArg
{
   template< typename T >
   static constexpr T getIdempotent() { return std::numeric_limits< T >::min(); };
   static constexpr T getIdempotent()
   {
      static_assert( std::numeric_limits< T >::is_specialized,
                     "std::numeric_limits is not specialized for the requested type" );
      return std::numeric_limits< T >::lowest();
   }

   template< typename Value, typename Index >
   constexpr void operator()( Value& lhs, const Value& rhs, Index& lhsIdx, const Index& rhsIdx ) const
@@ -121,7 +141,12 @@ struct MaxWithArg
struct LogicalAnd : public std::logical_and< void >
{
   template< typename T >
   static constexpr T getIdempotent() { return true; };
   static constexpr T getIdempotent()
   {
      static_assert( std::numeric_limits< T >::is_specialized,
                     "std::numeric_limits is not specialized for the requested type" );
      return std::numeric_limits< T >::max();
   }
};

/**
@@ -130,7 +155,7 @@ struct LogicalAnd : public std::logical_and< void >
struct LogicalOr : public std::logical_or< void >
{
   template< typename T >
   static constexpr T getIdempotent() { return false; };
   static constexpr T getIdempotent() { return 0; }
};

/**
@@ -139,7 +164,7 @@ struct LogicalOr : public std::logical_or< void >
struct BitAnd : public std::bit_and< void >
{
   template< typename T >
   static constexpr T getIdempotent() { return ~static_cast< T >( 0 ); };
   static constexpr T getIdempotent() { return ~static_cast< T >( 0 ); }
};

/**
@@ -148,7 +173,7 @@ struct BitAnd : public std::bit_and< void >
struct BitOr : public std::bit_or< void >
{
   template< typename T >
   static constexpr T getIdempotent() { return 0; };
   static constexpr T getIdempotent() { return 0; }
};

} // namespace TNL