Jump to content

  • Log In with Google      Sign In   
  • Create Account


#ActualÁlvaro

Posted 04 April 2013 - 11:11 AM

But is there any chance to use template specialisation here so I don't need a seperate function?

Of course there is, but it gets even uglier...
template <typename T>
bool contains_bits(T x, T y, std::true_type) {
  return (static_cast<typename std::underlying_type<T>::type>(x)
          & static_cast<typename std::underlying_type<T>::type>(y))
    == static_cast<typename std::underlying_type<T>::type>(y);
}

template <typename T>
bool contains_bits(T x, T y, std::false_type) {
  return (x & y) == y;
}

template <typename T>
bool contains_bits(T x, T y) {
  return contains_bits(x, y, std::integral_constant<bool, std::is_enum<T>::value>());
}

#1Álvaro

Posted 04 April 2013 - 11:10 AM

But is there any chance to use template specialisation here so I don't need a seperate function?

Of course there is, but it gets even uglier...
template <typename T>
bool contains(T x, T y, std::true_type) {
  return (static_cast<typename std::underlying_type<T>::type>(x)
          & static_cast<typename std::underlying_type<T>::type>(y))
    == static_cast<typename std::underlying_type<T>::type>(y);
}

template <typename T>
bool contains(T x, T y, std::false_type) {
  return (x & y) == y;
}

template <typename T>
bool contains_bits(T x, T y) {
  return contains(x, y, std::integral_constant<bool, std::is_enum<T>::value>());
}

PARTNERS