utility.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. // Utilities used throughout the library -*- C++ -*-
  2. // Copyright (C) 2004-2022 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file include/bits/utility.h
  21. * This is an internal header file, included by other library headers.
  22. * Do not attempt to use it directly. @headername{utility}
  23. *
  24. * This file contains the parts of `<utility>` needed by other headers,
  25. * so they don't need to include the whole of `<utility>`.
  26. */
  27. #ifndef _GLIBCXX_UTILITY_H
  28. #define _GLIBCXX_UTILITY_H 1
  29. #pragma GCC system_header
  30. #if __cplusplus >= 201103L
  31. #include <type_traits>
  32. #include <bits/move.h>
  33. namespace std _GLIBCXX_VISIBILITY(default)
  34. {
  35. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  36. /// Finds the size of a given tuple type.
  37. template<typename _Tp>
  38. struct tuple_size;
  39. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  40. // 2313. tuple_size should always derive from integral_constant<size_t, N>
  41. // 2770. tuple_size<const T> specialization is not SFINAE compatible
  42. template<typename _Tp,
  43. typename _Up = typename remove_cv<_Tp>::type,
  44. typename = typename enable_if<is_same<_Tp, _Up>::value>::type,
  45. size_t = tuple_size<_Tp>::value>
  46. using __enable_if_has_tuple_size = _Tp;
  47. template<typename _Tp>
  48. struct tuple_size<const __enable_if_has_tuple_size<_Tp>>
  49. : public tuple_size<_Tp> { };
  50. template<typename _Tp>
  51. struct tuple_size<volatile __enable_if_has_tuple_size<_Tp>>
  52. : public tuple_size<_Tp> { };
  53. template<typename _Tp>
  54. struct tuple_size<const volatile __enable_if_has_tuple_size<_Tp>>
  55. : public tuple_size<_Tp> { };
  56. #if __cplusplus >= 201703L
  57. template<typename _Tp>
  58. inline constexpr size_t tuple_size_v = tuple_size<_Tp>::value;
  59. #endif
  60. /// Gives the type of the ith element of a given tuple type.
  61. template<size_t __i, typename _Tp>
  62. struct tuple_element;
  63. // Duplicate of C++14's tuple_element_t for internal use in C++11 mode
  64. template<size_t __i, typename _Tp>
  65. using __tuple_element_t = typename tuple_element<__i, _Tp>::type;
  66. template<size_t __i, typename _Tp>
  67. struct tuple_element<__i, const _Tp>
  68. {
  69. typedef typename add_const<__tuple_element_t<__i, _Tp>>::type type;
  70. };
  71. template<size_t __i, typename _Tp>
  72. struct tuple_element<__i, volatile _Tp>
  73. {
  74. typedef typename add_volatile<__tuple_element_t<__i, _Tp>>::type type;
  75. };
  76. template<size_t __i, typename _Tp>
  77. struct tuple_element<__i, const volatile _Tp>
  78. {
  79. typedef typename add_cv<__tuple_element_t<__i, _Tp>>::type type;
  80. };
  81. #if __cplusplus >= 201402L
  82. // Return the index of _Tp in _Types, if it occurs exactly once.
  83. // Otherwise, return sizeof...(_Types).
  84. template<typename _Tp, typename... _Types>
  85. constexpr size_t
  86. __find_uniq_type_in_pack()
  87. {
  88. constexpr size_t __sz = sizeof...(_Types);
  89. constexpr bool __found[__sz] = { __is_same(_Tp, _Types) ... };
  90. size_t __n = __sz;
  91. for (size_t __i = 0; __i < __sz; ++__i)
  92. {
  93. if (__found[__i])
  94. {
  95. if (__n < __sz) // more than one _Tp found
  96. return __sz;
  97. __n = __i;
  98. }
  99. }
  100. return __n;
  101. }
  102. // The standard says this macro and alias template should be in <tuple> but we
  103. // define them here, to be available in <array>, <utility> and <ranges> too.
  104. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  105. // 3378. tuple_size_v/tuple_element_t should be available when
  106. // tuple_size/tuple_element are
  107. #define __cpp_lib_tuple_element_t 201402L
  108. template<size_t __i, typename _Tp>
  109. using tuple_element_t = typename tuple_element<__i, _Tp>::type;
  110. #endif // C++14
  111. // Stores a tuple of indices. Used by tuple and pair, and by bind() to
  112. // extract the elements in a tuple.
  113. template<size_t... _Indexes> struct _Index_tuple { };
  114. // Builds an _Index_tuple<0, 1, 2, ..., _Num-1>.
  115. template<size_t _Num>
  116. struct _Build_index_tuple
  117. {
  118. #if __has_builtin(__make_integer_seq)
  119. template<typename, size_t... _Indices>
  120. using _IdxTuple = _Index_tuple<_Indices...>;
  121. // Clang defines __make_integer_seq for this purpose.
  122. using __type = __make_integer_seq<_IdxTuple, size_t, _Num>;
  123. #else
  124. // For GCC and other compilers, use __integer_pack instead.
  125. using __type = _Index_tuple<__integer_pack(_Num)...>;
  126. #endif
  127. };
  128. #if __cplusplus >= 201402L
  129. #define __cpp_lib_integer_sequence 201304L
  130. /// Class template integer_sequence
  131. template<typename _Tp, _Tp... _Idx>
  132. struct integer_sequence
  133. {
  134. typedef _Tp value_type;
  135. static constexpr size_t size() noexcept { return sizeof...(_Idx); }
  136. };
  137. /// Alias template make_integer_sequence
  138. template<typename _Tp, _Tp _Num>
  139. using make_integer_sequence
  140. #if __has_builtin(__make_integer_seq)
  141. = __make_integer_seq<integer_sequence, _Tp, _Num>;
  142. #else
  143. = integer_sequence<_Tp, __integer_pack(_Num)...>;
  144. #endif
  145. /// Alias template index_sequence
  146. template<size_t... _Idx>
  147. using index_sequence = integer_sequence<size_t, _Idx...>;
  148. /// Alias template make_index_sequence
  149. template<size_t _Num>
  150. using make_index_sequence = make_integer_sequence<size_t, _Num>;
  151. /// Alias template index_sequence_for
  152. template<typename... _Types>
  153. using index_sequence_for = make_index_sequence<sizeof...(_Types)>;
  154. #if __cplusplus >= 201703L
  155. struct in_place_t {
  156. explicit in_place_t() = default;
  157. };
  158. inline constexpr in_place_t in_place{};
  159. template<typename _Tp> struct in_place_type_t
  160. {
  161. explicit in_place_type_t() = default;
  162. };
  163. template<typename _Tp>
  164. inline constexpr in_place_type_t<_Tp> in_place_type{};
  165. template<size_t _Idx> struct in_place_index_t
  166. {
  167. explicit in_place_index_t() = default;
  168. };
  169. template<size_t _Idx>
  170. inline constexpr in_place_index_t<_Idx> in_place_index{};
  171. template<typename>
  172. inline constexpr bool __is_in_place_type_v = false;
  173. template<typename _Tp>
  174. inline constexpr bool __is_in_place_type_v<in_place_type_t<_Tp>> = true;
  175. template<typename _Tp>
  176. using __is_in_place_type = bool_constant<__is_in_place_type_v<_Tp>>;
  177. #endif // C++17
  178. #endif // C++14
  179. template<size_t _Np, typename... _Types>
  180. struct _Nth_type
  181. { };
  182. template<typename _Tp0, typename... _Rest>
  183. struct _Nth_type<0, _Tp0, _Rest...>
  184. { using type = _Tp0; };
  185. template<typename _Tp0, typename _Tp1, typename... _Rest>
  186. struct _Nth_type<1, _Tp0, _Tp1, _Rest...>
  187. { using type = _Tp1; };
  188. template<typename _Tp0, typename _Tp1, typename _Tp2, typename... _Rest>
  189. struct _Nth_type<2, _Tp0, _Tp1, _Tp2, _Rest...>
  190. { using type = _Tp2; };
  191. template<size_t _Np, typename _Tp0, typename _Tp1, typename _Tp2,
  192. typename... _Rest>
  193. #if __cpp_concepts
  194. requires (_Np >= 3)
  195. #endif
  196. struct _Nth_type<_Np, _Tp0, _Tp1, _Tp2, _Rest...>
  197. : _Nth_type<_Np - 3, _Rest...>
  198. { };
  199. #if ! __cpp_concepts // Need additional specializations to avoid ambiguities.
  200. template<typename _Tp0, typename _Tp1, typename... _Rest>
  201. struct _Nth_type<0, _Tp0, _Tp1, _Rest...>
  202. { using type = _Tp0; };
  203. template<typename _Tp0, typename _Tp1, typename _Tp2, typename... _Rest>
  204. struct _Nth_type<0, _Tp0, _Tp1, _Tp2, _Rest...>
  205. { using type = _Tp0; };
  206. template<typename _Tp0, typename _Tp1, typename _Tp2, typename... _Rest>
  207. struct _Nth_type<1, _Tp0, _Tp1, _Tp2, _Rest...>
  208. { using type = _Tp1; };
  209. #endif
  210. _GLIBCXX_END_NAMESPACE_VERSION
  211. } // namespace
  212. #endif // C++11
  213. #endif /* _GLIBCXX_UTILITY_H */