integer_sequence.hpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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(_MSC_VER) || defined(__GNUC__)
  12. # pragma push_macro( "I" )
  13. # undef I
  14. #endif
  15. #if defined(__has_builtin)
  16. # if __has_builtin(__make_integer_seq)
  17. # define BOOST_MP11_HAS_MAKE_INTEGER_SEQ
  18. # endif
  19. #endif
  20. namespace boost
  21. {
  22. namespace mp11
  23. {
  24. // integer_sequence
  25. template<class T, T... I> struct integer_sequence
  26. {
  27. };
  28. #if defined(BOOST_MP11_HAS_MAKE_INTEGER_SEQ)
  29. template<class T, T N> using make_integer_sequence = __make_integer_seq<integer_sequence, T, N>;
  30. #else
  31. // detail::make_integer_sequence_impl
  32. namespace detail
  33. {
  34. // iseq_if_c
  35. template<bool C, class T, class E> struct iseq_if_c_impl;
  36. template<class T, class E> struct iseq_if_c_impl<true, T, E>
  37. {
  38. using type = T;
  39. };
  40. template<class T, class E> struct iseq_if_c_impl<false, T, E>
  41. {
  42. using type = E;
  43. };
  44. template<bool C, class T, class E> using iseq_if_c = typename iseq_if_c_impl<C, T, E>::type;
  45. // iseq_identity
  46. template<class T> struct iseq_identity
  47. {
  48. using type = T;
  49. };
  50. template<class S1, class S2> struct append_integer_sequence;
  51. template<class T, T... I, T... J> struct append_integer_sequence<integer_sequence<T, I...>, integer_sequence<T, J...>>
  52. {
  53. using type = integer_sequence< T, I..., ( J + sizeof...(I) )... >;
  54. };
  55. template<class T, T N> struct make_integer_sequence_impl;
  56. template<class T, T N> struct make_integer_sequence_impl_
  57. {
  58. private:
  59. static_assert( N >= 0, "make_integer_sequence<T, N>: N must not be negative" );
  60. static T const M = N / 2;
  61. static T const R = N % 2;
  62. using S1 = typename make_integer_sequence_impl<T, M>::type;
  63. using S2 = typename append_integer_sequence<S1, S1>::type;
  64. using S3 = typename make_integer_sequence_impl<T, R>::type;
  65. using S4 = typename append_integer_sequence<S2, S3>::type;
  66. public:
  67. using type = S4;
  68. };
  69. 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> > >
  70. {
  71. };
  72. } // namespace detail
  73. // make_integer_sequence
  74. template<class T, T N> using make_integer_sequence = typename detail::make_integer_sequence_impl<T, N>::type;
  75. #endif // defined(BOOST_MP11_HAS_MAKE_INTEGER_SEQ)
  76. // index_sequence
  77. template<std::size_t... I> using index_sequence = integer_sequence<std::size_t, I...>;
  78. // make_index_sequence
  79. template<std::size_t N> using make_index_sequence = make_integer_sequence<std::size_t, N>;
  80. // index_sequence_for
  81. template<class... T> using index_sequence_for = make_integer_sequence<std::size_t, sizeof...(T)>;
  82. } // namespace mp11
  83. } // namespace boost
  84. #if defined(_MSC_VER) || defined(__GNUC__)
  85. # pragma pop_macro( "I" )
  86. #endif
  87. #endif // #ifndef BOOST_MP11_INTEGER_SEQUENCE_HPP_INCLUDED