libstdc++
utility
Go to the documentation of this file.
1 // <utility> -*- C++ -*-
2 
3 // Copyright (C) 2001-2017 Free Software Foundation, Inc.
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
8 // Free Software Foundation; either version 3, or (at your option)
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 
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.
19 
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/>.
24 
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 
51 /** @file include/utility
52  * This is a Standard C++ Library header.
53  */
54 
55 #ifndef _GLIBCXX_UTILITY
56 #define _GLIBCXX_UTILITY 1
57 
58 #pragma GCC system_header
59 
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 
68 #include <bits/c++config.h>
69 #include <bits/stl_relops.h>
70 #include <bits/stl_pair.h>
71 
72 #if __cplusplus >= 201103L
73 
74 #include <type_traits>
75 #include <bits/move.h>
76 #include <initializer_list>
77 
78 #if __cplusplus > 201402L
79 #include <exception>
80 #endif
81 
82 namespace std _GLIBCXX_VISIBILITY(default)
83 {
84 _GLIBCXX_BEGIN_NAMESPACE_VERSION
85 
86  /// Finds the size of a given tuple type.
87  template<typename _Tp>
88  struct tuple_size;
89 
90  // _GLIBCXX_RESOLVE_LIB_DEFECTS
91  // 2770. tuple_size<const T> specialization is not SFINAE compatible
92  template<typename _Tp, typename = void>
93  struct __tuple_size_cv_impl { };
94 
95  template<typename _Tp>
96  struct __tuple_size_cv_impl<_Tp, __void_t<decltype(tuple_size<_Tp>::value)>>
97  : integral_constant<size_t, tuple_size<_Tp>::value> { };
98 
99  // _GLIBCXX_RESOLVE_LIB_DEFECTS
100  // 2313. tuple_size should always derive from integral_constant<size_t, N>
101  template<typename _Tp>
102  struct tuple_size<const _Tp> : __tuple_size_cv_impl<_Tp> { };
103 
104  template<typename _Tp>
105  struct tuple_size<volatile _Tp> : __tuple_size_cv_impl<_Tp> { };
106 
107  template<typename _Tp>
108  struct tuple_size<const volatile _Tp> : __tuple_size_cv_impl<_Tp> { };
109 
110  /// Gives the type of the ith element of a given tuple type.
111  template<std::size_t __i, typename _Tp>
112  struct tuple_element;
113 
114  // Duplicate of C++14's tuple_element_t for internal use in C++11 mode
115  template<std::size_t __i, typename _Tp>
116  using __tuple_element_t = typename tuple_element<__i, _Tp>::type;
117 
118  template<std::size_t __i, typename _Tp>
119  struct tuple_element<__i, const _Tp>
120  {
121  typedef typename add_const<__tuple_element_t<__i, _Tp>>::type type;
122  };
123 
124  template<std::size_t __i, typename _Tp>
125  struct tuple_element<__i, volatile _Tp>
126  {
127  typedef typename add_volatile<__tuple_element_t<__i, _Tp>>::type type;
128  };
129 
130  template<std::size_t __i, typename _Tp>
131  struct tuple_element<__i, const volatile _Tp>
132  {
133  typedef typename add_cv<__tuple_element_t<__i, _Tp>>::type type;
134  };
135 
136 #if __cplusplus > 201103L
137 #define __cpp_lib_tuple_element_t 201402
138 
139  template<std::size_t __i, typename _Tp>
140  using tuple_element_t = typename tuple_element<__i, _Tp>::type;
141 #endif
142 
143  // Various functions which give std::pair a tuple-like interface.
144 
145  /// Partial specialization for std::pair
146  template<typename _T1, typename _T2>
147  struct __is_tuple_like_impl<std::pair<_T1, _T2>> : true_type
148  { };
149 
150  /// Partial specialization for std::pair
151  template<class _Tp1, class _Tp2>
152  struct tuple_size<std::pair<_Tp1, _Tp2>>
153  : public integral_constant<std::size_t, 2> { };
154 
155  /// Partial specialization for std::pair
156  template<class _Tp1, class _Tp2>
157  struct tuple_element<0, std::pair<_Tp1, _Tp2>>
158  { typedef _Tp1 type; };
159 
160  /// Partial specialization for std::pair
161  template<class _Tp1, class _Tp2>
162  struct tuple_element<1, std::pair<_Tp1, _Tp2>>
163  { typedef _Tp2 type; };
164 
165  template<std::size_t _Int>
166  struct __pair_get;
167 
168  template<>
169  struct __pair_get<0>
170  {
171  template<typename _Tp1, typename _Tp2>
172  static constexpr _Tp1&
173  __get(std::pair<_Tp1, _Tp2>& __pair) noexcept
174  { return __pair.first; }
175 
176  template<typename _Tp1, typename _Tp2>
177  static constexpr _Tp1&&
178  __move_get(std::pair<_Tp1, _Tp2>&& __pair) noexcept
179  { return std::forward<_Tp1>(__pair.first); }
180 
181  template<typename _Tp1, typename _Tp2>
182  static constexpr const _Tp1&
183  __const_get(const std::pair<_Tp1, _Tp2>& __pair) noexcept
184  { return __pair.first; }
185  };
186 
187  template<>
188  struct __pair_get<1>
189  {
190  template<typename _Tp1, typename _Tp2>
191  static constexpr _Tp2&
192  __get(std::pair<_Tp1, _Tp2>& __pair) noexcept
193  { return __pair.second; }
194 
195  template<typename _Tp1, typename _Tp2>
196  static constexpr _Tp2&&
197  __move_get(std::pair<_Tp1, _Tp2>&& __pair) noexcept
198  { return std::forward<_Tp2>(__pair.second); }
199 
200  template<typename _Tp1, typename _Tp2>
201  static constexpr const _Tp2&
202  __const_get(const std::pair<_Tp1, _Tp2>& __pair) noexcept
203  { return __pair.second; }
204  };
205 
206  template<std::size_t _Int, class _Tp1, class _Tp2>
207  constexpr typename tuple_element<_Int, std::pair<_Tp1, _Tp2>>::type&
208  get(std::pair<_Tp1, _Tp2>& __in) noexcept
209  { return __pair_get<_Int>::__get(__in); }
210 
211  template<std::size_t _Int, class _Tp1, class _Tp2>
212  constexpr typename tuple_element<_Int, std::pair<_Tp1, _Tp2>>::type&&
213  get(std::pair<_Tp1, _Tp2>&& __in) noexcept
214  { return __pair_get<_Int>::__move_get(std::move(__in)); }
215 
216  template<std::size_t _Int, class _Tp1, class _Tp2>
217  constexpr const typename tuple_element<_Int, std::pair<_Tp1, _Tp2>>::type&
218  get(const std::pair<_Tp1, _Tp2>& __in) noexcept
219  { return __pair_get<_Int>::__const_get(__in); }
220 
221 #if __cplusplus > 201103L
222 
223 #define __cpp_lib_tuples_by_type 201304
224 
225  template <typename _Tp, typename _Up>
226  constexpr _Tp&
227  get(pair<_Tp, _Up>& __p) noexcept
228  { return __p.first; }
229 
230  template <typename _Tp, typename _Up>
231  constexpr const _Tp&
232  get(const pair<_Tp, _Up>& __p) noexcept
233  { return __p.first; }
234 
235  template <typename _Tp, typename _Up>
236  constexpr _Tp&&
237  get(pair<_Tp, _Up>&& __p) noexcept
238  { return std::move(__p.first); }
239 
240  template <typename _Tp, typename _Up>
241  constexpr _Tp&
242  get(pair<_Up, _Tp>& __p) noexcept
243  { return __p.second; }
244 
245  template <typename _Tp, typename _Up>
246  constexpr const _Tp&
247  get(const pair<_Up, _Tp>& __p) noexcept
248  { return __p.second; }
249 
250  template <typename _Tp, typename _Up>
251  constexpr _Tp&&
252  get(pair<_Up, _Tp>&& __p) noexcept
253  { return std::move(__p.second); }
254 
255 #define __cpp_lib_exchange_function 201304
256 
257  /// Assign @p __new_val to @p __obj and return its previous value.
258  template <typename _Tp, typename _Up = _Tp>
259  inline _Tp
260  exchange(_Tp& __obj, _Up&& __new_val)
261  { return std::__exchange(__obj, std::forward<_Up>(__new_val)); }
262 #endif
263 
264  // Stores a tuple of indices. Used by tuple and pair, and by bind() to
265  // extract the elements in a tuple.
266  template<size_t... _Indexes> struct _Index_tuple { };
267 
268  // Concatenates two _Index_tuples.
269  template<typename _Itup1, typename _Itup2> struct _Itup_cat;
270 
271  template<size_t... _Ind1, size_t... _Ind2>
272  struct _Itup_cat<_Index_tuple<_Ind1...>, _Index_tuple<_Ind2...>>
273  {
274  using __type = _Index_tuple<_Ind1..., (_Ind2 + sizeof...(_Ind1))...>;
275  };
276 
277  // Builds an _Index_tuple<0, 1, 2, ..., _Num-1>.
278  template<size_t _Num>
279  struct _Build_index_tuple
280  : _Itup_cat<typename _Build_index_tuple<_Num / 2>::__type,
281  typename _Build_index_tuple<_Num - _Num / 2>::__type>
282  { };
283 
284  template<>
285  struct _Build_index_tuple<1>
286  {
287  typedef _Index_tuple<0> __type;
288  };
289 
290  template<>
291  struct _Build_index_tuple<0>
292  {
293  typedef _Index_tuple<> __type;
294  };
295 
296 #if __cplusplus > 201103L
297 
298 #define __cpp_lib_integer_sequence 201304
299 
300  /// Class template integer_sequence
301  template<typename _Tp, _Tp... _Idx>
302  struct integer_sequence
303  {
304  typedef _Tp value_type;
305  static constexpr size_t size() { return sizeof...(_Idx); }
306  };
307 
308  template<typename _Tp, _Tp _Num,
309  typename _ISeq = typename _Build_index_tuple<_Num>::__type>
310  struct _Make_integer_sequence;
311 
312  template<typename _Tp, _Tp _Num, size_t... _Idx>
313  struct _Make_integer_sequence<_Tp, _Num, _Index_tuple<_Idx...>>
314  {
315  static_assert( _Num >= 0,
316  "Cannot make integer sequence of negative length" );
317 
318  typedef integer_sequence<_Tp, static_cast<_Tp>(_Idx)...> __type;
319  };
320 
321  /// Alias template make_integer_sequence
322  template<typename _Tp, _Tp _Num>
323  using make_integer_sequence
324  = typename _Make_integer_sequence<_Tp, _Num>::__type;
325 
326  /// Alias template index_sequence
327  template<size_t... _Idx>
328  using index_sequence = integer_sequence<size_t, _Idx...>;
329 
330  /// Alias template make_index_sequence
331  template<size_t _Num>
332  using make_index_sequence = make_integer_sequence<size_t, _Num>;
333 
334  /// Alias template index_sequence_for
335  template<typename... _Types>
336  using index_sequence_for = make_index_sequence<sizeof...(_Types)>;
337 #endif
338 
339 #if __cplusplus > 201402L
340 
341  struct in_place_t {
342  explicit in_place_t() = default;
343  };
344 
345  inline constexpr in_place_t in_place{};
346 
347  template<typename _Tp> struct in_place_type_t
348  {
349  explicit in_place_type_t() = default;
350  };
351 
352  template<typename _Tp>
353  inline constexpr in_place_type_t<_Tp> in_place_type{};
354 
355  template<size_t _Idx> struct in_place_index_t
356  {
357  explicit in_place_index_t() = default;
358  };
359 
360  template<size_t _Idx>
361  inline constexpr in_place_index_t<_Idx> in_place_index{};
362 
363  template<typename>
364  struct __is_in_place_type_impl : false_type
365  { };
366 
367  template<typename _Tp>
368  struct __is_in_place_type_impl<in_place_type_t<_Tp>> : true_type
369  { };
370 
371  template<typename _Tp>
372  struct __is_in_place_type
373  : public __is_in_place_type_impl<_Tp>
374  { };
375 
376 #define __cpp_lib_as_const 201510
377  template<typename _Tp>
378  constexpr add_const_t<_Tp>& as_const(_Tp& __t) noexcept { return __t; }
379 
380  template<typename _Tp>
381  void as_const(const _Tp&&) = delete;
382 
383 #endif // C++17
384 
385 _GLIBCXX_END_NAMESPACE_VERSION
386 } // namespace
387 
388 #endif
389 
390 #endif /* _GLIBCXX_UTILITY */