Skip to content

Instantly share code, notes, and snippets.

@remyroez
Last active November 12, 2016 05:55
Show Gist options
  • Save remyroez/3f755f3b171119f27f9115e5e5e44d5a to your computer and use it in GitHub Desktop.
Save remyroez/3f755f3b171119f27f9115e5e5e44d5a to your computer and use it in GitHub Desktop.
Logical Operator Type Traits
#include <type_traits>
// impl ----------
#if (__cplusplus > 201402L)
using std::bool_constant;
using std::negation;
using std::conjunction;
using std::disjunction;
#else
template <bool B>
using bool_constant = std::integral_constant<bool, B>;
template <class T>
using negation = bool_constant<!T::value>;
template <class...>
struct conjunction {};
template <class T, class... Args>
struct conjunction<T, Args...> : bool_constant<T::value and conjunction<Args...>::value> {};
template <>
struct conjunction<> : std::true_type {};
template <class...>
struct disjunction {};
template <class T, class... Args>
struct disjunction<T, Args...> : bool_constant<T::value or disjunction<Args...>::value> {};
template <>
struct disjunction<> : std::false_type {};
#endif
// usage ----------
template <typename T, typename... Ts>
using is_same_all = conjunction<std::is_same<T, Ts>...>;
template <typename T, typename... Ts>
using is_same_either = disjunction<std::is_same<T, Ts>...>;
int main()
{
static_assert(!is_same_all<int, int, int, int, int>::value, "all int");
static_assert( is_same_all<int, int, int, int, float>::value, "not all int");
static_assert(!is_same_either<int, bool, char, int, float>::value, "has int");
return 0;
}
@remyroez
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment