integer_sequence.hpp 2.9 KB

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