integer_sequence.hpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #ifndef BOOST_MP11_INTEGER_SEQUENCE_HPP_INCLUDED
  2. #define BOOST_MP11_INTEGER_SEQUENCE_HPP_INCLUDED
  3. // Copyright 2015, 2017, 2019 Peter Dimov.
  4. //
  5. // Distributed under the Boost Software License, Version 1.0.
  6. //
  7. // See accompanying file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt
  9. #include <boost/mp11/version.hpp>
  10. #include <cstddef>
  11. #if defined(__has_builtin)
  12. # if __has_builtin(__make_integer_seq)
  13. # define BOOST_MP11_HAS_MAKE_INTEGER_SEQ
  14. # endif
  15. #endif
  16. namespace boost
  17. {
  18. namespace mp11
  19. {
  20. // integer_sequence
  21. template<class T, T... I> struct integer_sequence
  22. {
  23. };
  24. #if defined(BOOST_MP11_HAS_MAKE_INTEGER_SEQ)
  25. template<class T, T N> using make_integer_sequence = __make_integer_seq<integer_sequence, T, N>;
  26. #else
  27. // detail::make_integer_sequence_impl
  28. namespace detail
  29. {
  30. // iseq_if_c
  31. template<bool C, class T, class E> struct iseq_if_c_impl;
  32. template<class T, class E> struct iseq_if_c_impl<true, T, E>
  33. {
  34. using type = T;
  35. };
  36. template<class T, class E> struct iseq_if_c_impl<false, T, E>
  37. {
  38. using type = E;
  39. };
  40. template<bool C, class T, class E> using iseq_if_c = typename iseq_if_c_impl<C, T, E>::type;
  41. // iseq_identity
  42. template<class T> struct iseq_identity
  43. {
  44. using type = T;
  45. };
  46. template<class S1, class S2> struct append_integer_sequence;
  47. template<class T, T... I, T... J> struct append_integer_sequence<integer_sequence<T, I...>, integer_sequence<T, J...>>
  48. {
  49. using type = integer_sequence< T, I..., ( J + sizeof...(I) )... >;
  50. };
  51. template<class T, T N> struct make_integer_sequence_impl;
  52. template<class T, T N> struct make_integer_sequence_impl_
  53. {
  54. private:
  55. static_assert( N >= 0, "make_integer_sequence<T, N>: N must not be negative" );
  56. static T const M = N / 2;
  57. static T const R = N % 2;
  58. using S1 = typename make_integer_sequence_impl<T, M>::type;
  59. using S2 = typename append_integer_sequence<S1, S1>::type;
  60. using S3 = typename make_integer_sequence_impl<T, R>::type;
  61. using S4 = typename append_integer_sequence<S2, S3>::type;
  62. public:
  63. using type = S4;
  64. };
  65. template<class T, T N> struct make_integer_sequence_impl: iseq_if_c<N == 0, iseq_identity<integer_sequence<T>>, iseq_if_c<N == 1, iseq_identity<integer_sequence<T, 0>>, make_integer_sequence_impl_<T, N> > >
  66. {
  67. };
  68. } // namespace detail
  69. // make_integer_sequence
  70. template<class T, T N> using make_integer_sequence = typename detail::make_integer_sequence_impl<T, N>::type;
  71. #endif // defined(BOOST_MP11_HAS_MAKE_INTEGER_SEQ)
  72. // index_sequence
  73. template<std::size_t... I> using index_sequence = integer_sequence<std::size_t, I...>;
  74. // make_index_sequence
  75. template<std::size_t N> using make_index_sequence = make_integer_sequence<std::size_t, N>;
  76. // index_sequence_for
  77. template<class... T> using index_sequence_for = make_integer_sequence<std::size_t, sizeof...(T)>;
  78. } // namespace mp11
  79. } // namespace boost
  80. #endif // #ifndef BOOST_MP11_INTEGER_SEQUENCE_HPP_INCLUDED