Created
July 27, 2021 14:20
-
-
Save Jihadist/204ddefa98c0eddf84246d6137859d1e to your computer and use it in GitHub Desktop.
simple variadic type_traits
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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