Created
March 25, 2016 17:25
-
-
Save insertinterestingnamehere/08672e94b35e736f5ebd to your computer and use it in GitHub Desktop.
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 <type_traits> | |
#include <typeinfo> | |
template <typename T, T... I> | |
struct integer_sequence { | |
enum { size = sizeof...(I) }; | |
typedef T value_type; | |
}; | |
template <typename... T> | |
struct type_sequence { | |
enum { size = sizeof...(T) }; | |
}; | |
template <typename I, size_t J> | |
struct to; | |
template <typename T, T I0, T... I> | |
struct to<integer_sequence<T, I0, I...>, 1> { | |
typedef integer_sequence<T, I0> type; | |
}; | |
template <typename... S> | |
struct outer; | |
template <typename T0, T0 I0, typename T1, T1... I1> | |
struct outer<integer_sequence<T0, I0>, integer_sequence<T1, I1...>> { | |
typedef type_sequence<integer_sequence<typename std::common_type<T0, T1>::type, I0, I1>...> type; | |
}; | |
template <typename S0, typename S1> | |
struct outer<S0, S1> { | |
typedef typename outer<typename to<S0, 1>::type, S1>::type type; | |
}; | |
#define PRINT_TYPE(T) std::cout << typeid(std::declval<T>()).name() << std::endl; | |
int main() { | |
// Commenting out these first two lines changes the output of the second two lines. | |
typedef typename outer<integer_sequence<int, 0>, integer_sequence<int, 1, 2>>::type example1; | |
PRINT_TYPE(example1); | |
typedef typename outer<integer_sequence<int, 0, 1>, integer_sequence<int, 2, 3>>::type example2; | |
PRINT_TYPE(example2); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment