integer_sequence.h 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #ifndef _LIBCPP___UTILITY_INTEGER_SEQUENCE_H
  9. #define _LIBCPP___UTILITY_INTEGER_SEQUENCE_H
  10. #include <__config>
  11. #include <type_traits>
  12. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  13. # pragma GCC system_header
  14. #endif
  15. _LIBCPP_BEGIN_NAMESPACE_STD
  16. #if _LIBCPP_STD_VER > 11
  17. template<class _Tp, _Tp... _Ip>
  18. struct _LIBCPP_TEMPLATE_VIS integer_sequence
  19. {
  20. typedef _Tp value_type;
  21. static_assert( is_integral<_Tp>::value,
  22. "std::integer_sequence can only be instantiated with an integral type" );
  23. static
  24. _LIBCPP_INLINE_VISIBILITY
  25. constexpr
  26. size_t
  27. size() noexcept { return sizeof...(_Ip); }
  28. };
  29. template<size_t... _Ip>
  30. using index_sequence = integer_sequence<size_t, _Ip...>;
  31. #if __has_builtin(__make_integer_seq) && !defined(_LIBCPP_TESTING_FALLBACK_MAKE_INTEGER_SEQUENCE)
  32. template <class _Tp, _Tp _Ep>
  33. using __make_integer_sequence _LIBCPP_NODEBUG = __make_integer_seq<integer_sequence, _Tp, _Ep>;
  34. #else
  35. template <class _Tp, class _T> struct __integer_sequence_convert {
  36. using type = integer_sequence<_Tp>;
  37. };
  38. template<class _Tp, class _Tp2, _Tp... _Values>
  39. struct __integer_sequence_convert<_Tp, __integer_sequence<_Tp2, _Values...>> {
  40. using type = integer_sequence<_Tp, _Values...>;
  41. };
  42. template<typename _Tp, _Tp _Np> using __make_integer_sequence_unchecked _LIBCPP_NODEBUG =
  43. typename __integer_sequence_convert<_Tp, typename __detail::__make<_Np>::type>::type;
  44. template <class _Tp, _Tp _Ep>
  45. struct __make_integer_sequence_checked
  46. {
  47. static_assert(is_integral<_Tp>::value,
  48. "std::make_integer_sequence can only be instantiated with an integral type" );
  49. static_assert(0 <= _Ep, "std::make_integer_sequence must have a non-negative sequence length");
  50. #ifdef _LIBCPP_COMPILER_MSVC
  51. #pragma warning ( push )
  52. #pragma warning ( disable : 4296 )
  53. #endif
  54. // Workaround GCC bug by preventing bad installations when 0 <= _Ep
  55. // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=68929
  56. typedef _LIBCPP_NODEBUG __make_integer_sequence_unchecked<_Tp, 0 <= _Ep ? _Ep : 0> type;
  57. #ifdef _LIBCPP_COMPILER_MSVC
  58. #pragma warning ( pop )
  59. #endif
  60. };
  61. template <class _Tp, _Tp _Ep>
  62. using __make_integer_sequence _LIBCPP_NODEBUG = typename __make_integer_sequence_checked<_Tp, _Ep>::type;
  63. #endif
  64. template<class _Tp, _Tp _Np>
  65. using make_integer_sequence = __make_integer_sequence<_Tp, _Np>;
  66. template<size_t _Np>
  67. using make_index_sequence = make_integer_sequence<size_t, _Np>;
  68. template<class... _Tp>
  69. using index_sequence_for = make_index_sequence<sizeof...(_Tp)>;
  70. #endif // _LIBCPP_STD_VER > 11
  71. _LIBCPP_END_NAMESPACE_STD
  72. #endif // _LIBCPP___UTILITY_INTEGER_SEQUENCE_H