integer_sequence.hpp 3.0 KB

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