]>
Commit | Line | Data |
---|---|---|
54c1bf78 | 1 | // <utility> -*- C++ -*- |
de96ac46 | 2 | |
cbe34bb5 | 3 | // Copyright (C) 2001-2017 Free Software Foundation, Inc. |
de96ac46 BK |
4 | // |
5 | // This file is part of the GNU ISO C++ Library. This library is free | |
6 | // software; you can redistribute it and/or modify it under the | |
7 | // terms of the GNU General Public License as published by the | |
748086b7 | 8 | // Free Software Foundation; either version 3, or (at your option) |
de96ac46 BK |
9 | // any later version. |
10 | ||
11 | // This library is distributed in the hope that it will be useful, | |
12 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | // GNU General Public License for more details. | |
15 | ||
748086b7 JJ |
16 | // Under Section 7 of GPL version 3, you are granted additional |
17 | // permissions described in the GCC Runtime Library Exception, version | |
18 | // 3.1, as published by the Free Software Foundation. | |
de96ac46 | 19 | |
748086b7 JJ |
20 | // You should have received a copy of the GNU General Public License and |
21 | // a copy of the GCC Runtime Library Exception along with this program; | |
22 | // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see | |
23 | // <http://www.gnu.org/licenses/>. | |
de96ac46 | 24 | |
54c1bf78 BK |
25 | /* |
26 | * | |
27 | * Copyright (c) 1994 | |
28 | * Hewlett-Packard Company | |
29 | * | |
30 | * Permission to use, copy, modify, distribute and sell this software | |
31 | * and its documentation for any purpose is hereby granted without fee, | |
32 | * provided that the above copyright notice appear in all copies and | |
33 | * that both that copyright notice and this permission notice appear | |
34 | * in supporting documentation. Hewlett-Packard Company makes no | |
35 | * representations about the suitability of this software for any | |
36 | * purpose. It is provided "as is" without express or implied warranty. | |
37 | * | |
38 | * | |
39 | * Copyright (c) 1996,1997 | |
40 | * Silicon Graphics Computer Systems, Inc. | |
41 | * | |
42 | * Permission to use, copy, modify, distribute and sell this software | |
43 | * and its documentation for any purpose is hereby granted without fee, | |
44 | * provided that the above copyright notice appear in all copies and | |
45 | * that both that copyright notice and this permission notice appear | |
46 | * in supporting documentation. Silicon Graphics makes no | |
47 | * representations about the suitability of this software for any | |
48 | * purpose. It is provided "as is" without express or implied warranty. | |
49 | */ | |
50 | ||
143c27b0 | 51 | /** @file include/utility |
33ac58d5 | 52 | * This is a Standard C++ Library header. |
2f9d51b8 PE |
53 | */ |
54 | ||
1143680e SE |
55 | #ifndef _GLIBCXX_UTILITY |
56 | #define _GLIBCXX_UTILITY 1 | |
54c1bf78 BK |
57 | |
58 | #pragma GCC system_header | |
285b36d6 | 59 | |
8e32aa11 BK |
60 | /** |
61 | * @defgroup utilities Utilities | |
62 | * | |
63 | * Components deemed generally useful. Includes pair, tuple, | |
64 | * forward/move helpers, ratio, function object, metaprogramming and | |
65 | * type traits, time, date, and memory functions. | |
66 | */ | |
67 | ||
54c1bf78 BK |
68 | #include <bits/c++config.h> |
69 | #include <bits/stl_relops.h> | |
70 | #include <bits/stl_pair.h> | |
71 | ||
734f5023 | 72 | #if __cplusplus >= 201103L |
a15f7cb8 | 73 | |
7d17de7f | 74 | #include <type_traits> |
53dc5044 PC |
75 | #include <bits/move.h> |
76 | #include <initializer_list> | |
77 | ||
25a69162 VV |
78 | #if __cplusplus > 201402L |
79 | #include <exception> | |
80 | #endif | |
81 | ||
12ffa228 BK |
82 | namespace std _GLIBCXX_VISIBILITY(default) |
83 | { | |
84 | _GLIBCXX_BEGIN_NAMESPACE_VERSION | |
53dc5044 | 85 | |
de0830e1 VV |
86 | /// Finds the size of a given tuple type. |
87 | template<typename _Tp> | |
88 | struct tuple_size; | |
89 | ||
90 | // _GLIBCXX_RESOLVE_LIB_DEFECTS | |
f859f912 | 91 | // 2313. tuple_size should always derive from integral_constant<size_t, N> |
6ae2ae3b | 92 | // 2770. tuple_size<const T> specialization is not SFINAE compatible |
6ae2ae3b | 93 | |
f859f912 JW |
94 | template<typename _Tp, |
95 | typename _Up = typename remove_cv<_Tp>::type, | |
96 | typename = typename enable_if<is_same<_Tp, _Up>::value>::type, | |
97 | size_t = tuple_size<_Tp>::value> | |
98 | using __enable_if_has_tuple_size = _Tp; | |
de0830e1 | 99 | |
6ae2ae3b | 100 | template<typename _Tp> |
f859f912 JW |
101 | struct tuple_size<const __enable_if_has_tuple_size<_Tp>> |
102 | : public tuple_size<_Tp> { }; | |
6ae2ae3b | 103 | |
de0830e1 | 104 | template<typename _Tp> |
f859f912 JW |
105 | struct tuple_size<volatile __enable_if_has_tuple_size<_Tp>> |
106 | : public tuple_size<_Tp> { }; | |
de0830e1 VV |
107 | |
108 | template<typename _Tp> | |
f859f912 JW |
109 | struct tuple_size<const volatile __enable_if_has_tuple_size<_Tp>> |
110 | : public tuple_size<_Tp> { }; | |
de0830e1 VV |
111 | |
112 | /// Gives the type of the ith element of a given tuple type. | |
113 | template<std::size_t __i, typename _Tp> | |
114 | struct tuple_element; | |
115 | ||
116 | // Duplicate of C++14's tuple_element_t for internal use in C++11 mode | |
117 | template<std::size_t __i, typename _Tp> | |
118 | using __tuple_element_t = typename tuple_element<__i, _Tp>::type; | |
119 | ||
120 | template<std::size_t __i, typename _Tp> | |
121 | struct tuple_element<__i, const _Tp> | |
122 | { | |
123 | typedef typename add_const<__tuple_element_t<__i, _Tp>>::type type; | |
124 | }; | |
125 | ||
126 | template<std::size_t __i, typename _Tp> | |
127 | struct tuple_element<__i, volatile _Tp> | |
128 | { | |
129 | typedef typename add_volatile<__tuple_element_t<__i, _Tp>>::type type; | |
130 | }; | |
131 | ||
132 | template<std::size_t __i, typename _Tp> | |
133 | struct tuple_element<__i, const volatile _Tp> | |
134 | { | |
135 | typedef typename add_cv<__tuple_element_t<__i, _Tp>>::type type; | |
136 | }; | |
137 | ||
138 | #if __cplusplus > 201103L | |
139 | #define __cpp_lib_tuple_element_t 201402 | |
140 | ||
141 | template<std::size_t __i, typename _Tp> | |
142 | using tuple_element_t = typename tuple_element<__i, _Tp>::type; | |
143 | #endif | |
53dc5044 | 144 | |
7d17de7f FD |
145 | // Various functions which give std::pair a tuple-like interface. |
146 | ||
147 | /// Partial specialization for std::pair | |
148 | template<typename _T1, typename _T2> | |
149 | struct __is_tuple_like_impl<std::pair<_T1, _T2>> : true_type | |
150 | { }; | |
3a004764 JW |
151 | |
152 | /// Partial specialization for std::pair | |
53dc5044 | 153 | template<class _Tp1, class _Tp2> |
7933dc2a | 154 | struct tuple_size<std::pair<_Tp1, _Tp2>> |
664e12c1 | 155 | : public integral_constant<std::size_t, 2> { }; |
53dc5044 | 156 | |
3a004764 | 157 | /// Partial specialization for std::pair |
53dc5044 | 158 | template<class _Tp1, class _Tp2> |
7933dc2a | 159 | struct tuple_element<0, std::pair<_Tp1, _Tp2>> |
53dc5044 | 160 | { typedef _Tp1 type; }; |
33ac58d5 | 161 | |
3a004764 | 162 | /// Partial specialization for std::pair |
53dc5044 | 163 | template<class _Tp1, class _Tp2> |
7933dc2a | 164 | struct tuple_element<1, std::pair<_Tp1, _Tp2>> |
53dc5044 PC |
165 | { typedef _Tp2 type; }; |
166 | ||
167 | template<std::size_t _Int> | |
168 | struct __pair_get; | |
169 | ||
170 | template<> | |
171 | struct __pair_get<0> | |
172 | { | |
173 | template<typename _Tp1, typename _Tp2> | |
9480716c | 174 | static constexpr _Tp1& |
7933dc2a | 175 | __get(std::pair<_Tp1, _Tp2>& __pair) noexcept |
18eeaec4 PC |
176 | { return __pair.first; } |
177 | ||
18eeaec4 | 178 | template<typename _Tp1, typename _Tp2> |
9480716c | 179 | static constexpr _Tp1&& |
18eeaec4 PC |
180 | __move_get(std::pair<_Tp1, _Tp2>&& __pair) noexcept |
181 | { return std::forward<_Tp1>(__pair.first); } | |
53dc5044 PC |
182 | |
183 | template<typename _Tp1, typename _Tp2> | |
9480716c | 184 | static constexpr const _Tp1& |
7933dc2a | 185 | __const_get(const std::pair<_Tp1, _Tp2>& __pair) noexcept |
18eeaec4 | 186 | { return __pair.first; } |
ccbbf8df VV |
187 | |
188 | template<typename _Tp1, typename _Tp2> | |
189 | static constexpr const _Tp1&& | |
190 | __const_move_get(const std::pair<_Tp1, _Tp2>&& __pair) noexcept | |
191 | { return std::forward<const _Tp1>(__pair.first); } | |
53dc5044 PC |
192 | }; |
193 | ||
194 | template<> | |
195 | struct __pair_get<1> | |
196 | { | |
197 | template<typename _Tp1, typename _Tp2> | |
9480716c | 198 | static constexpr _Tp2& |
7933dc2a | 199 | __get(std::pair<_Tp1, _Tp2>& __pair) noexcept |
18eeaec4 PC |
200 | { return __pair.second; } |
201 | ||
18eeaec4 | 202 | template<typename _Tp1, typename _Tp2> |
9480716c | 203 | static constexpr _Tp2&& |
18eeaec4 PC |
204 | __move_get(std::pair<_Tp1, _Tp2>&& __pair) noexcept |
205 | { return std::forward<_Tp2>(__pair.second); } | |
53dc5044 PC |
206 | |
207 | template<typename _Tp1, typename _Tp2> | |
9480716c | 208 | static constexpr const _Tp2& |
7933dc2a | 209 | __const_get(const std::pair<_Tp1, _Tp2>& __pair) noexcept |
18eeaec4 | 210 | { return __pair.second; } |
ccbbf8df VV |
211 | |
212 | template<typename _Tp1, typename _Tp2> | |
213 | static constexpr const _Tp2&& | |
214 | __const_move_get(const std::pair<_Tp1, _Tp2>&& __pair) noexcept | |
215 | { return std::forward<const _Tp2>(__pair.second); } | |
53dc5044 PC |
216 | }; |
217 | ||
218 | template<std::size_t _Int, class _Tp1, class _Tp2> | |
9480716c | 219 | constexpr typename tuple_element<_Int, std::pair<_Tp1, _Tp2>>::type& |
7933dc2a | 220 | get(std::pair<_Tp1, _Tp2>& __in) noexcept |
53dc5044 PC |
221 | { return __pair_get<_Int>::__get(__in); } |
222 | ||
18eeaec4 | 223 | template<std::size_t _Int, class _Tp1, class _Tp2> |
9480716c | 224 | constexpr typename tuple_element<_Int, std::pair<_Tp1, _Tp2>>::type&& |
18eeaec4 PC |
225 | get(std::pair<_Tp1, _Tp2>&& __in) noexcept |
226 | { return __pair_get<_Int>::__move_get(std::move(__in)); } | |
18eeaec4 | 227 | |
53dc5044 | 228 | template<std::size_t _Int, class _Tp1, class _Tp2> |
9480716c | 229 | constexpr const typename tuple_element<_Int, std::pair<_Tp1, _Tp2>>::type& |
7933dc2a | 230 | get(const std::pair<_Tp1, _Tp2>& __in) noexcept |
53dc5044 PC |
231 | { return __pair_get<_Int>::__const_get(__in); } |
232 | ||
ccbbf8df VV |
233 | template<std::size_t _Int, class _Tp1, class _Tp2> |
234 | constexpr const typename tuple_element<_Int, std::pair<_Tp1, _Tp2>>::type&& | |
235 | get(const std::pair<_Tp1, _Tp2>&& __in) noexcept | |
236 | { return __pair_get<_Int>::__const_move_get(std::move(__in)); } | |
237 | ||
c98b0598 | 238 | #if __cplusplus > 201103L |
a15f7cb8 ESR |
239 | |
240 | #define __cpp_lib_tuples_by_type 201304 | |
241 | ||
b5a8fed6 JW |
242 | template <typename _Tp, typename _Up> |
243 | constexpr _Tp& | |
244 | get(pair<_Tp, _Up>& __p) noexcept | |
245 | { return __p.first; } | |
246 | ||
247 | template <typename _Tp, typename _Up> | |
248 | constexpr const _Tp& | |
249 | get(const pair<_Tp, _Up>& __p) noexcept | |
250 | { return __p.first; } | |
251 | ||
252 | template <typename _Tp, typename _Up> | |
253 | constexpr _Tp&& | |
254 | get(pair<_Tp, _Up>&& __p) noexcept | |
255 | { return std::move(__p.first); } | |
256 | ||
ccbbf8df VV |
257 | template <typename _Tp, typename _Up> |
258 | constexpr const _Tp&& | |
259 | get(const pair<_Tp, _Up>&& __p) noexcept | |
260 | { return std::move(__p.first); } | |
261 | ||
b5a8fed6 JW |
262 | template <typename _Tp, typename _Up> |
263 | constexpr _Tp& | |
264 | get(pair<_Up, _Tp>& __p) noexcept | |
265 | { return __p.second; } | |
266 | ||
267 | template <typename _Tp, typename _Up> | |
268 | constexpr const _Tp& | |
269 | get(const pair<_Up, _Tp>& __p) noexcept | |
270 | { return __p.second; } | |
271 | ||
272 | template <typename _Tp, typename _Up> | |
273 | constexpr _Tp&& | |
274 | get(pair<_Up, _Tp>&& __p) noexcept | |
275 | { return std::move(__p.second); } | |
276 | ||
ccbbf8df VV |
277 | template <typename _Tp, typename _Up> |
278 | constexpr const _Tp&& | |
279 | get(const pair<_Up, _Tp>&& __p) noexcept | |
280 | { return std::move(__p.second); } | |
281 | ||
a15f7cb8 ESR |
282 | #define __cpp_lib_exchange_function 201304 |
283 | ||
c98b0598 | 284 | /// Assign @p __new_val to @p __obj and return its previous value. |
b5a8fed6 | 285 | template <typename _Tp, typename _Up = _Tp> |
c98b0598 JW |
286 | inline _Tp |
287 | exchange(_Tp& __obj, _Up&& __new_val) | |
9b817548 | 288 | { return std::__exchange(__obj, std::forward<_Up>(__new_val)); } |
c98b0598 JW |
289 | #endif |
290 | ||
1b1bdc03 JW |
291 | // Stores a tuple of indices. Used by tuple and pair, and by bind() to |
292 | // extract the elements in a tuple. | |
5f0b7c95 JW |
293 | template<size_t... _Indexes> struct _Index_tuple { }; |
294 | ||
db624b3b JW |
295 | #ifdef __has_builtin |
296 | # if __has_builtin(__make_integer_seq) | |
297 | # define _GLIBCXX_USE_MAKE_INTEGER_SEQ 1 | |
298 | # endif | |
299 | #endif | |
1b1bdc03 JW |
300 | |
301 | // Builds an _Index_tuple<0, 1, 2, ..., _Num-1>. | |
302 | template<size_t _Num> | |
303 | struct _Build_index_tuple | |
304 | { | |
db624b3b JW |
305 | #if _GLIBCXX_USE_MAKE_INTEGER_SEQ |
306 | template<typename, size_t... _Indices> | |
307 | using _IdxTuple = _Index_tuple<_Indices...>; | |
1b1bdc03 | 308 | |
db624b3b JW |
309 | using __type = __make_integer_seq<_IdxTuple, size_t, _Num>; |
310 | #else | |
311 | using __type = _Index_tuple<__integer_pack(_Num)...>; | |
312 | #endif | |
1b1bdc03 JW |
313 | }; |
314 | ||
315 | #if __cplusplus > 201103L | |
a15f7cb8 ESR |
316 | |
317 | #define __cpp_lib_integer_sequence 201304 | |
318 | ||
1b1bdc03 JW |
319 | /// Class template integer_sequence |
320 | template<typename _Tp, _Tp... _Idx> | |
321 | struct integer_sequence | |
322 | { | |
323 | typedef _Tp value_type; | |
324 | static constexpr size_t size() { return sizeof...(_Idx); } | |
325 | }; | |
326 | ||
1b1bdc03 JW |
327 | /// Alias template make_integer_sequence |
328 | template<typename _Tp, _Tp _Num> | |
329 | using make_integer_sequence | |
db624b3b JW |
330 | #if _GLIBCXX_USE_MAKE_INTEGER_SEQ |
331 | = __make_integer_seq<integer_sequence, _Tp, _Num>; | |
332 | #else | |
333 | = integer_sequence<_Tp, __integer_pack(_Num)...>; | |
334 | #endif | |
335 | ||
336 | #undef _GLIBCXX_USE_MAKE_INTEGER_SEQ | |
1b1bdc03 JW |
337 | |
338 | /// Alias template index_sequence | |
339 | template<size_t... _Idx> | |
340 | using index_sequence = integer_sequence<size_t, _Idx...>; | |
341 | ||
342 | /// Alias template make_index_sequence | |
343 | template<size_t _Num> | |
344 | using make_index_sequence = make_integer_sequence<size_t, _Num>; | |
345 | ||
346 | /// Alias template index_sequence_for | |
347 | template<typename... _Types> | |
348 | using index_sequence_for = make_index_sequence<sizeof...(_Types)>; | |
349 | #endif | |
350 | ||
25a69162 VV |
351 | #if __cplusplus > 201402L |
352 | ||
627a2f59 VV |
353 | struct in_place_t { |
354 | explicit in_place_t() = default; | |
25a69162 VV |
355 | }; |
356 | ||
627a2f59 VV |
357 | inline constexpr in_place_t in_place{}; |
358 | ||
359 | template<typename _Tp> struct in_place_type_t | |
360 | { | |
361 | explicit in_place_type_t() = default; | |
362 | }; | |
363 | ||
364 | template<typename _Tp> | |
365 | inline constexpr in_place_type_t<_Tp> in_place_type{}; | |
366 | ||
367 | template<size_t _Idx> struct in_place_index_t | |
368 | { | |
369 | explicit in_place_index_t() = default; | |
370 | }; | |
371 | ||
372 | template<size_t _Idx> | |
373 | inline constexpr in_place_index_t<_Idx> in_place_index{}; | |
25a69162 | 374 | |
5c578ae4 VV |
375 | template<typename> |
376 | struct __is_in_place_type_impl : false_type | |
377 | { }; | |
378 | ||
379 | template<typename _Tp> | |
380 | struct __is_in_place_type_impl<in_place_type_t<_Tp>> : true_type | |
381 | { }; | |
382 | ||
383 | template<typename _Tp> | |
384 | struct __is_in_place_type | |
385 | : public __is_in_place_type_impl<_Tp> | |
386 | { }; | |
387 | ||
32eaac9c JW |
388 | #define __cpp_lib_as_const 201510 |
389 | template<typename _Tp> | |
390 | constexpr add_const_t<_Tp>& as_const(_Tp& __t) noexcept { return __t; } | |
391 | ||
392 | template<typename _Tp> | |
393 | void as_const(const _Tp&&) = delete; | |
394 | ||
5c578ae4 | 395 | #endif // C++17 |
25a69162 | 396 | |
12ffa228 BK |
397 | _GLIBCXX_END_NAMESPACE_VERSION |
398 | } // namespace | |
53dc5044 | 399 | |
af13a7a6 BK |
400 | #endif |
401 | ||
1143680e | 402 | #endif /* _GLIBCXX_UTILITY */ |