Skip to content

Instantly share code, notes, and snippets.

@Jihadist
Created July 27, 2021 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jihadist/204ddefa98c0eddf84246d6137859d1e to your computer and use it in GitHub Desktop.
Save Jihadist/204ddefa98c0eddf84246d6137859d1e to your computer and use it in GitHub Desktop.
simple variadic type_traits
#include <iostream>
#include <tuple>
#include <type_traits>
#include <utility>
template <bool... Bs> using bool_sequence = std::integer_sequence<bool, Bs...>;
template <bool... Bs>
using bool_and =
std::is_same<bool_sequence<Bs...>, bool_sequence<(Bs || true)...>>;
template <bool... Bs>
using bool_or = std::integral_constant<bool, !bool_and<!Bs...>::value>;
template <typename R, bool... Bs> // note: R first, no default :(
using enable_if_any = std::enable_if<bool_or<Bs...>::value, R>;
template <typename R, bool... Bs> // note: R first, no default :(
using enable_if_all = std::enable_if<bool_and<Bs...>::value, R>;
template <typename T, typename... Ts>
using are_same = bool_and<std::is_same<T, Ts>::value...>;
using namespace std;
int main() {
cout << "Hello World!" << endl;
cout << are_same<int, int, int>::value << endl;
cout << are_same<int, int, float>::value << endl;
cout << are_same<int, float, float>::value << endl;
cout << are_same<float, float, float>::value << endl;
cout << are_same<float, int, float>::value << endl;
cout << are_same<float, int, int>::value << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment