3 Adapts `std::integer_sequence` for use with Hana.
5 @copyright Louis Dionne 2013-2017
6 Distributed under the Boost Software License, Version 1.0.
7 (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
10 #ifndef BOOST_HANA_EXT_STD_INTEGER_SEQUENCE_HPP
11 #define BOOST_HANA_EXT_STD_INTEGER_SEQUENCE_HPP
13 #include <boost/hana/bool.hpp>
14 #include <boost/hana/config.hpp>
15 #include <boost/hana/detail/fast_and.hpp>
16 #include <boost/hana/ext/std/integral_constant.hpp>
17 #include <boost/hana/fwd/at.hpp>
18 #include <boost/hana/fwd/core/tag_of.hpp>
19 #include <boost/hana/fwd/drop_front.hpp>
20 #include <boost/hana/fwd/equal.hpp>
21 #include <boost/hana/fwd/is_empty.hpp>
22 #include <boost/hana/fwd/unpack.hpp>
25 #include <type_traits>
29 #ifdef BOOST_HANA_DOXYGEN_INVOKED
31 //! @ingroup group-ext-std
32 //! Adaptation of `std::integer_sequence` for Hana.
39 //! Two `std::integer_sequence`s are equal if and only if they have the
40 //! same number of elements, and if corresponding elements compare equal.
41 //! The types of the elements held in both `integer_sequence`s may be
42 //! different, as long as they can be compared.
43 //! @include example/ext/std/integer_sequence/comparable.cpp
46 //! Folding an `integer_sequence` is equivalent to folding a sequence of
47 //! `std::integral_constant`s with the corresponding types.
48 //! @include example/ext/std/integer_sequence/foldable.cpp
51 //! Iterating over an `integer_sequence` is equivalent to iterating over
52 //! a sequence of the corresponding `std::integral_constant`s.
53 //! @include example/ext/std/integer_sequence/iterable.cpp
56 //! Searching through an `integer_sequence` is equivalent to searching
57 //! through the corresponding sequence of `std::integral_constant`s.
58 //! @include example/ext/std/integer_sequence/searchable.cpp
59 template <typename T, T ...v>
60 struct integer_sequence { };
65 BOOST_HANA_NAMESPACE_BEGIN
66 namespace ext { namespace std { struct integer_sequence_tag; }}
68 template <typename T, T ...v>
69 struct tag_of<std::integer_sequence<T, v...>> {
70 using type = ext::std::integer_sequence_tag;
73 //////////////////////////////////////////////////////////////////////////
75 //////////////////////////////////////////////////////////////////////////
77 struct equal_impl<ext::std::integer_sequence_tag, ext::std::integer_sequence_tag> {
78 template <typename X, X ...xs, typename Y, Y ...ys>
79 static constexpr hana::bool_<detail::fast_and<(xs == ys)...>::value>
80 apply(std::integer_sequence<X, xs...> const&, std::integer_sequence<Y, ys...> const&)
83 template <typename Xs, typename Ys>
84 static constexpr hana::false_ apply(Xs const&, Ys const&, ...)
88 //////////////////////////////////////////////////////////////////////////
90 //////////////////////////////////////////////////////////////////////////
92 struct unpack_impl<ext::std::integer_sequence_tag> {
93 template <typename T, T ...v, typename F>
94 static constexpr decltype(auto)
95 apply(std::integer_sequence<T, v...> const&, F&& f) {
96 return static_cast<F&&>(f)(std::integral_constant<T, v>{}...);
100 //////////////////////////////////////////////////////////////////////////
102 //////////////////////////////////////////////////////////////////////////
104 struct at_impl<ext::std::integer_sequence_tag> {
105 template <typename T, T ...v, typename N>
106 static constexpr auto apply(std::integer_sequence<T, v...> const&, N const&) {
107 constexpr std::size_t n = N::value;
108 constexpr T values_[] = {v...};
109 return std::integral_constant<T, values_[n]>{};
114 struct drop_front_impl<ext::std::integer_sequence_tag> {
115 template <std::size_t n, typename T, T ...t, std::size_t ...i>
116 static constexpr auto drop_front_helper(std::integer_sequence<T, t...>,
117 std::index_sequence<i...>)
119 constexpr T ts[sizeof...(t)+1] = {t...}; // avoid 0-sized array
120 return std::integer_sequence<T, ts[n + i]...>{};
123 template <typename T, T ...t, typename N>
124 static constexpr auto apply(std::integer_sequence<T, t...> ts, N const&) {
125 constexpr std::size_t n = N::value;
126 constexpr std::size_t len = sizeof...(t);
127 return drop_front_helper<n>(ts,
128 std::make_index_sequence<(n < len ? len - n : 0)>{});
133 struct is_empty_impl<ext::std::integer_sequence_tag> {
134 template <typename T, T ...xs>
135 static constexpr auto apply(std::integer_sequence<T, xs...> const&)
136 { return hana::bool_c<sizeof...(xs) == 0>; }
138 BOOST_HANA_NAMESPACE_END
140 #endif // !BOOST_HANA_EXT_STD_INTEGER_SEQUENCE_HPP