Created
October 28, 2014 22:34
-
-
Save orlp/ae824952a9d493735580 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 <cstddef> | |
template<class T, T... I> | |
struct integer_sequence { | |
using value_type = T; | |
static constexpr std::size_t size() noexcept { return sizeof...(I); } | |
}; | |
namespace detail { | |
template<class, class> struct Combine; | |
template<class T, T ...I, T ...J> | |
struct Combine<integer_sequence<T, I...>, integer_sequence<T, J...>> { | |
using type = integer_sequence<T, I..., (sizeof...(I) + J)...>; | |
}; | |
template<class T, T N, int = (N == 0 || N == 1) ? N : 2> | |
struct Seq { | |
static_assert(N >= 0, "N must be nonnegative in make_integer_sequence<T, N>"); | |
using type = typename Combine<typename Seq<T, N/2>::type, | |
typename Seq<T, N - N/2>::type>::type; | |
}; | |
template<class T, T N> struct Seq<T, N, 0> { using type = integer_sequence<T>; }; | |
template<class T, T N> struct Seq<T, N, 1> { using type = integer_sequence<T, 0>; }; | |
}; | |
template<class T, T N> using make_integer_sequence = typename detail::Seq<T, N>::type; | |
template<std::size_t... I> using index_sequence = integer_sequence<std::size_t, I...>; | |
template<std::size_t N> using make_index_sequence = make_integer_sequence<std::size_t, N>; | |
template<class... T> using index_sequence_for = make_index_sequence<sizeof...(T)>; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment