1 // <utility> -*- C++ -*-
3 // Copyright (C) 2001-2017 Free Software Foundation, Inc.
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
8 // Free Software Foundation; either version 3, or (at your option)
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.
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.
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/>.
28 * Hewlett-Packard Company
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.
39 * Copyright (c) 1996,1997
40 * Silicon Graphics Computer Systems, Inc.
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.
51 /** @file include/utility
52 * This is a Standard C++ Library header.
55 #ifndef _GLIBCXX_UTILITY
56 #define _GLIBCXX_UTILITY 1
58 #pragma GCC system_header
61 * @defgroup utilities Utilities
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.
68 #include <bits/c++config.h>
69 #include <bits/stl_relops.h>
70 #include <bits/stl_pair.h>
72 #if __cplusplus >= 201103L
74 #include <type_traits>
75 #include <bits/move.h>
76 #include <initializer_list>
78 #if __cplusplus > 201402L
82 namespace std _GLIBCXX_VISIBILITY(default)
84 _GLIBCXX_BEGIN_NAMESPACE_VERSION
86 /// Finds the size of a given tuple type.
87 template<typename _Tp>
90 // _GLIBCXX_RESOLVE_LIB_DEFECTS
91 // 2313. tuple_size should always derive from integral_constant<size_t, N>
92 // 2770. tuple_size<const T> specialization is not SFINAE compatible
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;
100 template<typename _Tp>
101 struct tuple_size<const __enable_if_has_tuple_size<_Tp>>
102 : public tuple_size<_Tp> { };
104 template<typename _Tp>
105 struct tuple_size<volatile __enable_if_has_tuple_size<_Tp>>
106 : public tuple_size<_Tp> { };
108 template<typename _Tp>
109 struct tuple_size<const volatile __enable_if_has_tuple_size<_Tp>>
110 : public tuple_size<_Tp> { };
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;
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;
120 template<std::size_t __i, typename _Tp>
121 struct tuple_element<__i, const _Tp>
123 typedef typename add_const<__tuple_element_t<__i, _Tp>>::type type;
126 template<std::size_t __i, typename _Tp>
127 struct tuple_element<__i, volatile _Tp>
129 typedef typename add_volatile<__tuple_element_t<__i, _Tp>>::type type;
132 template<std::size_t __i, typename _Tp>
133 struct tuple_element<__i, const volatile _Tp>
135 typedef typename add_cv<__tuple_element_t<__i, _Tp>>::type type;
138 #if __cplusplus > 201103L
139 #define __cpp_lib_tuple_element_t 201402
141 template<std::size_t __i, typename _Tp>
142 using tuple_element_t = typename tuple_element<__i, _Tp>::type;
145 // Various functions which give std::pair a tuple-like interface.
147 /// Partial specialization for std::pair
148 template<typename _T1, typename _T2>
149 struct __is_tuple_like_impl<std::pair<_T1, _T2>> : true_type
152 /// Partial specialization for std::pair
153 template<class _Tp1, class _Tp2>
154 struct tuple_size<std::pair<_Tp1, _Tp2>>
155 : public integral_constant<std::size_t, 2> { };
157 /// Partial specialization for std::pair
158 template<class _Tp1, class _Tp2>
159 struct tuple_element<0, std::pair<_Tp1, _Tp2>>
160 { typedef _Tp1 type; };
162 /// Partial specialization for std::pair
163 template<class _Tp1, class _Tp2>
164 struct tuple_element<1, std::pair<_Tp1, _Tp2>>
165 { typedef _Tp2 type; };
167 template<std::size_t _Int>
173 template<typename _Tp1, typename _Tp2>
174 static constexpr _Tp1&
175 __get(std::pair<_Tp1, _Tp2>& __pair) noexcept
176 { return __pair.first; }
178 template<typename _Tp1, typename _Tp2>
179 static constexpr _Tp1&&
180 __move_get(std::pair<_Tp1, _Tp2>&& __pair) noexcept
181 { return std::forward<_Tp1>(__pair.first); }
183 template<typename _Tp1, typename _Tp2>
184 static constexpr const _Tp1&
185 __const_get(const std::pair<_Tp1, _Tp2>& __pair) noexcept
186 { return __pair.first; }
192 template<typename _Tp1, typename _Tp2>
193 static constexpr _Tp2&
194 __get(std::pair<_Tp1, _Tp2>& __pair) noexcept
195 { return __pair.second; }
197 template<typename _Tp1, typename _Tp2>
198 static constexpr _Tp2&&
199 __move_get(std::pair<_Tp1, _Tp2>&& __pair) noexcept
200 { return std::forward<_Tp2>(__pair.second); }
202 template<typename _Tp1, typename _Tp2>
203 static constexpr const _Tp2&
204 __const_get(const std::pair<_Tp1, _Tp2>& __pair) noexcept
205 { return __pair.second; }
208 template<std::size_t _Int, class _Tp1, class _Tp2>
209 constexpr typename tuple_element<_Int, std::pair<_Tp1, _Tp2>>::type&
210 get(std::pair<_Tp1, _Tp2>& __in) noexcept
211 { return __pair_get<_Int>::__get(__in); }
213 template<std::size_t _Int, class _Tp1, class _Tp2>
214 constexpr typename tuple_element<_Int, std::pair<_Tp1, _Tp2>>::type&&
215 get(std::pair<_Tp1, _Tp2>&& __in) noexcept
216 { return __pair_get<_Int>::__move_get(std::move(__in)); }
218 template<std::size_t _Int, class _Tp1, class _Tp2>
219 constexpr const typename tuple_element<_Int, std::pair<_Tp1, _Tp2>>::type&
220 get(const std::pair<_Tp1, _Tp2>& __in) noexcept
221 { return __pair_get<_Int>::__const_get(__in); }
223 #if __cplusplus > 201103L
225 #define __cpp_lib_tuples_by_type 201304
227 template <typename _Tp, typename _Up>
229 get(pair<_Tp, _Up>& __p) noexcept
230 { return __p.first; }
232 template <typename _Tp, typename _Up>
234 get(const pair<_Tp, _Up>& __p) noexcept
235 { return __p.first; }
237 template <typename _Tp, typename _Up>
239 get(pair<_Tp, _Up>&& __p) noexcept
240 { return std::move(__p.first); }
242 template <typename _Tp, typename _Up>
244 get(pair<_Up, _Tp>& __p) noexcept
245 { return __p.second; }
247 template <typename _Tp, typename _Up>
249 get(const pair<_Up, _Tp>& __p) noexcept
250 { return __p.second; }
252 template <typename _Tp, typename _Up>
254 get(pair<_Up, _Tp>&& __p) noexcept
255 { return std::move(__p.second); }
257 #define __cpp_lib_exchange_function 201304
259 /// Assign @p __new_val to @p __obj and return its previous value.
260 template <typename _Tp, typename _Up = _Tp>
262 exchange(_Tp& __obj, _Up&& __new_val)
263 { return std::__exchange(__obj, std::forward<_Up>(__new_val)); }
266 // Stores a tuple of indices. Used by tuple and pair, and by bind() to
267 // extract the elements in a tuple.
268 template<size_t... _Indexes> struct _Index_tuple { };
270 // Concatenates two _Index_tuples.
271 template<typename _Itup1, typename _Itup2> struct _Itup_cat;
273 template<size_t... _Ind1, size_t... _Ind2>
274 struct _Itup_cat<_Index_tuple<_Ind1...>, _Index_tuple<_Ind2...>>
276 using __type = _Index_tuple<_Ind1..., (_Ind2 + sizeof...(_Ind1))...>;
279 // Builds an _Index_tuple<0, 1, 2, ..., _Num-1>.
280 template<size_t _Num>
281 struct _Build_index_tuple
282 : _Itup_cat<typename _Build_index_tuple<_Num / 2>::__type,
283 typename _Build_index_tuple<_Num - _Num / 2>::__type>
287 struct _Build_index_tuple<1>
289 typedef _Index_tuple<0> __type;
293 struct _Build_index_tuple<0>
295 typedef _Index_tuple<> __type;
298 #if __cplusplus > 201103L
300 #define __cpp_lib_integer_sequence 201304
302 /// Class template integer_sequence
303 template<typename _Tp, _Tp... _Idx>
304 struct integer_sequence
306 typedef _Tp value_type;
307 static constexpr size_t size() { return sizeof...(_Idx); }
310 template<typename _Tp, _Tp _Num,
311 typename _ISeq = typename _Build_index_tuple<_Num>::__type>
312 struct _Make_integer_sequence;
314 template<typename _Tp, _Tp _Num, size_t... _Idx>
315 struct _Make_integer_sequence<_Tp, _Num, _Index_tuple<_Idx...>>
317 static_assert( _Num >= 0,
318 "Cannot make integer sequence of negative length" );
320 typedef integer_sequence<_Tp, static_cast<_Tp>(_Idx)...> __type;
323 /// Alias template make_integer_sequence
324 template<typename _Tp, _Tp _Num>
325 using make_integer_sequence
326 = typename _Make_integer_sequence<_Tp, _Num>::__type;
328 /// Alias template index_sequence
329 template<size_t... _Idx>
330 using index_sequence = integer_sequence<size_t, _Idx...>;
332 /// Alias template make_index_sequence
333 template<size_t _Num>
334 using make_index_sequence = make_integer_sequence<size_t, _Num>;
336 /// Alias template index_sequence_for
337 template<typename... _Types>
338 using index_sequence_for = make_index_sequence<sizeof...(_Types)>;
341 #if __cplusplus > 201402L
344 explicit in_place_t() = default;
347 inline constexpr in_place_t in_place{};
349 template<typename _Tp> struct in_place_type_t
351 explicit in_place_type_t() = default;
354 template<typename _Tp>
355 inline constexpr in_place_type_t<_Tp> in_place_type{};
357 template<size_t _Idx> struct in_place_index_t
359 explicit in_place_index_t() = default;
362 template<size_t _Idx>
363 inline constexpr in_place_index_t<_Idx> in_place_index{};
366 struct __is_in_place_type_impl : false_type
369 template<typename _Tp>
370 struct __is_in_place_type_impl<in_place_type_t<_Tp>> : true_type
373 template<typename _Tp>
374 struct __is_in_place_type
375 : public __is_in_place_type_impl<_Tp>
378 #define __cpp_lib_as_const 201510
379 template<typename _Tp>
380 constexpr add_const_t<_Tp>& as_const(_Tp& __t) noexcept { return __t; }
382 template<typename _Tp>
383 void as_const(const _Tp&&) = delete;
387 _GLIBCXX_END_NAMESPACE_VERSION
392 #endif /* _GLIBCXX_UTILITY */