2 // Copyright (c) 2016-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
4 // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 // Official repository: https://github.com/boostorg/beast
10 #ifndef BOOST_BEAST_DETAIL_INTEGER_SEQUENCE_HPP
11 #define BOOST_BEAST_DETAIL_INTEGER_SEQUENCE_HPP
13 #include <boost/config.hpp>
15 #include <type_traits>
22 template<class T, T... Ints>
23 struct integer_sequence
26 BOOST_STATIC_ASSERT(std::is_integral<T>::value);
28 static std::size_t constexpr static_size = sizeof...(Ints);
30 static std::size_t constexpr size()
32 return sizeof...(Ints);
36 template<std::size_t... Ints>
37 using index_sequence = integer_sequence<std::size_t, Ints...>;
39 // This workaround is needed for broken sizeof...
40 template<class... Args>
41 struct sizeof_workaround
43 static std::size_t constexpr size = sizeof... (Args);
48 // This implementation compiles on real MSVC and clang but not gcc
50 template<class T, unsigned long long N, class Seq>
51 struct make_integer_sequence_unchecked;
53 template<class T, unsigned long long N, unsigned long long ...Indices>
54 struct make_integer_sequence_unchecked<
55 T, N, integer_sequence<T, Indices...>>
57 using type = typename make_integer_sequence_unchecked<
58 T, N-1, integer_sequence<T, N-1, Indices...>>::type;
61 template<class T, unsigned long long ...Indices>
62 struct make_integer_sequence_unchecked<
63 T, 0, integer_sequence<T, Indices...>>
65 using type = integer_sequence<T, Indices...>;
68 template<class T, T N>
69 struct make_integer_sequence_checked
71 BOOST_STATIC_ASSERT(std::is_integral<T>::value);
72 BOOST_STATIC_ASSERT(N >= 0);
74 using type = typename make_integer_sequence_unchecked<
75 T, N, integer_sequence<T>>::type;
78 template<class T, T N>
79 using make_integer_sequence =
80 typename make_integer_sequence_checked<T, N>::type;
82 template<std::size_t N>
83 using make_index_sequence = make_integer_sequence<std::size_t, N>;
85 template<class... Args>
86 using index_sequence_for =
87 make_index_sequence<sizeof_workaround<Args...>::size>;
91 // This implementation compiles on gcc but not MSVC
93 template<std::size_t... Ints>
96 using next = index_tuple<Ints..., sizeof... (Ints)>;
100 template<std::size_t N>
101 struct build_index_tuple
103 using type = typename build_index_tuple<N-1>::type::next;
107 struct build_index_tuple<0>
109 using type = index_tuple<>;
112 template<class T, T N,
113 class Seq = typename build_index_tuple<N>::type
115 struct integer_sequence_helper;
117 template<class T, T N, std::size_t... Ints>
118 struct integer_sequence_helper<T, N, index_tuple<Ints...>>
120 BOOST_STATIC_ASSERT(std::is_integral<T>::value);
121 BOOST_STATIC_ASSERT(N >= 0);
123 using type = integer_sequence<T, static_cast<T> (Ints)...>;
126 template<class T, T N>
127 using make_integer_sequence =
128 typename integer_sequence_helper<T, N>::type;
130 template<std::size_t N>
131 using make_index_sequence = make_integer_sequence<std::size_t, N>;
133 template<class... Args>
134 using index_sequence_for =
135 make_index_sequence<sizeof_workaround<Args...>::size>;