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