libstdc++
stl_pair.h
Go to the documentation of this file.
1 // Pair implementation -*- 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 bits/stl_pair.h
52  * This is an internal header file, included by other library headers.
53  * Do not attempt to use it directly. @headername{utility}
54  */
55 
56 #ifndef _STL_PAIR_H
57 #define _STL_PAIR_H 1
58 
59 #include <bits/move.h> // for std::move / std::forward, and std::swap
60 
61 #if __cplusplus >= 201103L
62 #include <type_traits> // for std::__decay_and_strip too
63 #endif
64 
65 namespace std _GLIBCXX_VISIBILITY(default)
66 {
67 _GLIBCXX_BEGIN_NAMESPACE_VERSION
68 
69  /**
70  * @addtogroup utilities
71  * @{
72  */
73 
74 #if __cplusplus >= 201103L
75  /// piecewise_construct_t
76  struct piecewise_construct_t { explicit piecewise_construct_t() = default; };
77 
78  /// piecewise_construct
79  _GLIBCXX17_INLINE constexpr piecewise_construct_t piecewise_construct =
81 
82  // Forward declarations.
83  template<typename...>
84  class tuple;
85 
86  template<std::size_t...>
87  struct _Index_tuple;
88 
89  // Concept utility functions, reused in conditionally-explicit
90  // constructors.
91  // See PR 70437, don't look at is_constructible or
92  // is_convertible if the types are the same to
93  // avoid querying those properties for incomplete types.
94  template <bool, typename _T1, typename _T2>
95  struct _PCC
96  {
97  template <typename _U1, typename _U2>
98  static constexpr bool _ConstructiblePair()
99  {
100  return __and_<is_constructible<_T1, const _U1&>,
101  is_constructible<_T2, const _U2&>>::value;
102  }
103 
104  template <typename _U1, typename _U2>
105  static constexpr bool _ImplicitlyConvertiblePair()
106  {
107  return __and_<is_convertible<const _U1&, _T1>,
108  is_convertible<const _U2&, _T2>>::value;
109  }
110 
111  template <typename _U1, typename _U2>
112  static constexpr bool _MoveConstructiblePair()
113  {
114  return __and_<is_constructible<_T1, _U1&&>,
115  is_constructible<_T2, _U2&&>>::value;
116  }
117 
118  template <typename _U1, typename _U2>
119  static constexpr bool _ImplicitlyMoveConvertiblePair()
120  {
121  return __and_<is_convertible<_U1&&, _T1>,
122  is_convertible<_U2&&, _T2>>::value;
123  }
124 
125  template <bool __implicit, typename _U1, typename _U2>
126  static constexpr bool _CopyMovePair()
127  {
128  using __do_converts = __and_<is_convertible<const _U1&, _T1>,
129  is_convertible<_U2&&, _T2>>;
130  using __converts = typename conditional<__implicit,
131  __do_converts,
132  __not_<__do_converts>>::type;
133  return __and_<is_constructible<_T1, const _U1&>,
134  is_constructible<_T2, _U2&&>,
135  __converts
136  >::value;
137  }
138 
139  template <bool __implicit, typename _U1, typename _U2>
140  static constexpr bool _MoveCopyPair()
141  {
142  using __do_converts = __and_<is_convertible<_U1&&, _T1>,
143  is_convertible<const _U2&, _T2>>;
144  using __converts = typename conditional<__implicit,
145  __do_converts,
146  __not_<__do_converts>>::type;
147  return __and_<is_constructible<_T1, _U1&&>,
148  is_constructible<_T2, const _U2&&>,
149  __converts
150  >::value;
151  }
152  };
153 
154  template <typename _T1, typename _T2>
155  struct _PCC<false, _T1, _T2>
156  {
157  template <typename _U1, typename _U2>
158  static constexpr bool _ConstructiblePair()
159  {
160  return false;
161  }
162 
163  template <typename _U1, typename _U2>
164  static constexpr bool _ImplicitlyConvertiblePair()
165  {
166  return false;
167  }
168 
169  template <typename _U1, typename _U2>
170  static constexpr bool _MoveConstructiblePair()
171  {
172  return false;
173  }
174 
175  template <typename _U1, typename _U2>
176  static constexpr bool _ImplicitlyMoveConvertiblePair()
177  {
178  return false;
179  }
180  };
181 
182 #endif
183 
184  /**
185  * @brief Struct holding two objects of arbitrary type.
186  *
187  * @tparam _T1 Type of first object.
188  * @tparam _T2 Type of second object.
189  */
190  template<typename _T1, typename _T2>
191  struct pair
192  {
193  typedef _T1 first_type; /// @c first_type is the first bound type
194  typedef _T2 second_type; /// @c second_type is the second bound type
195 
196  _T1 first; /// @c first is a copy of the first object
197  _T2 second; /// @c second is a copy of the second object
198 
199  // _GLIBCXX_RESOLVE_LIB_DEFECTS
200  // 265. std::pair::pair() effects overly restrictive
201  /** The default constructor creates @c first and @c second using their
202  * respective default constructors. */
203 #if __cplusplus >= 201103L
204  template <typename _U1 = _T1,
205  typename _U2 = _T2,
206  typename enable_if<__and_<
207  __is_implicitly_default_constructible<_U1>,
208  __is_implicitly_default_constructible<_U2>>
209  ::value, bool>::type = true>
210 #endif
211  _GLIBCXX_CONSTEXPR pair()
212  : first(), second() { }
213 
214 #if __cplusplus >= 201103L
215  template <typename _U1 = _T1,
216  typename _U2 = _T2,
217  typename enable_if<__and_<
218  is_default_constructible<_U1>,
219  is_default_constructible<_U2>,
220  __not_<
221  __and_<__is_implicitly_default_constructible<_U1>,
222  __is_implicitly_default_constructible<_U2>>>>
223  ::value, bool>::type = false>
224  explicit constexpr pair()
225  : first(), second() { }
226 #endif
227 
228  /** Two objects may be passed to a @c pair constructor to be copied. */
229 #if __cplusplus < 201103L
230  pair(const _T1& __a, const _T2& __b)
231  : first(__a), second(__b) { }
232 #else
233  // Shortcut for constraining the templates that don't take pairs.
234  using _PCCP = _PCC<true, _T1, _T2>;
235 
236  template<typename _U1 = _T1, typename _U2=_T2, typename
237  enable_if<_PCCP::template
238  _ConstructiblePair<_U1, _U2>()
239  && _PCCP::template
240  _ImplicitlyConvertiblePair<_U1, _U2>(),
241  bool>::type=true>
242  constexpr pair(const _T1& __a, const _T2& __b)
243  : first(__a), second(__b) { }
244 
245  template<typename _U1 = _T1, typename _U2=_T2, typename
246  enable_if<_PCCP::template
247  _ConstructiblePair<_U1, _U2>()
248  && !_PCCP::template
249  _ImplicitlyConvertiblePair<_U1, _U2>(),
250  bool>::type=false>
251  explicit constexpr pair(const _T1& __a, const _T2& __b)
252  : first(__a), second(__b) { }
253 #endif
254 
255  /** There is also a templated copy ctor for the @c pair class itself. */
256 #if __cplusplus < 201103L
257  template<typename _U1, typename _U2>
258  pair(const pair<_U1, _U2>& __p)
259  : first(__p.first), second(__p.second) { }
260 #else
261  // Shortcut for constraining the templates that take pairs.
262  template <typename _U1, typename _U2>
263  using _PCCFP = _PCC<!is_same<_T1, _U1>::value
264  || !is_same<_T2, _U2>::value,
265  _T1, _T2>;
266 
267  template<typename _U1, typename _U2, typename
268  enable_if<_PCCFP<_U1, _U2>::template
269  _ConstructiblePair<_U1, _U2>()
271  _ImplicitlyConvertiblePair<_U1, _U2>(),
272  bool>::type=true>
273  constexpr pair(const pair<_U1, _U2>& __p)
274  : first(__p.first), second(__p.second) { }
275 
276  template<typename _U1, typename _U2, typename
277  enable_if<_PCCFP<_U1, _U2>::template
278  _ConstructiblePair<_U1, _U2>()
280  _ImplicitlyConvertiblePair<_U1, _U2>(),
281  bool>::type=false>
282  explicit constexpr pair(const pair<_U1, _U2>& __p)
283  : first(__p.first), second(__p.second) { }
284 
285  constexpr pair(const pair&) = default;
286  constexpr pair(pair&&) = default;
287 
288  // DR 811.
289  template<typename _U1, typename
290  enable_if<_PCCP::template
291  _MoveCopyPair<true, _U1, _T2>(),
292  bool>::type=true>
293  constexpr pair(_U1&& __x, const _T2& __y)
294  : first(std::forward<_U1>(__x)), second(__y) { }
295 
296  template<typename _U1, typename
297  enable_if<_PCCP::template
298  _MoveCopyPair<false, _U1, _T2>(),
299  bool>::type=false>
300  explicit constexpr pair(_U1&& __x, const _T2& __y)
301  : first(std::forward<_U1>(__x)), second(__y) { }
302 
303  template<typename _U2, typename
304  enable_if<_PCCP::template
305  _CopyMovePair<true, _T1, _U2>(),
306  bool>::type=true>
307  constexpr pair(const _T1& __x, _U2&& __y)
308  : first(__x), second(std::forward<_U2>(__y)) { }
309 
310  template<typename _U2, typename
311  enable_if<_PCCP::template
312  _CopyMovePair<false, _T1, _U2>(),
313  bool>::type=false>
314  explicit pair(const _T1& __x, _U2&& __y)
315  : first(__x), second(std::forward<_U2>(__y)) { }
316 
317  template<typename _U1, typename _U2, typename
318  enable_if<_PCCP::template
319  _MoveConstructiblePair<_U1, _U2>()
320  && _PCCP::template
321  _ImplicitlyMoveConvertiblePair<_U1, _U2>(),
322  bool>::type=true>
323  constexpr pair(_U1&& __x, _U2&& __y)
324  : first(std::forward<_U1>(__x)), second(std::forward<_U2>(__y)) { }
325 
326  template<typename _U1, typename _U2, typename
327  enable_if<_PCCP::template
328  _MoveConstructiblePair<_U1, _U2>()
329  && !_PCCP::template
330  _ImplicitlyMoveConvertiblePair<_U1, _U2>(),
331  bool>::type=false>
332  explicit constexpr pair(_U1&& __x, _U2&& __y)
333  : first(std::forward<_U1>(__x)), second(std::forward<_U2>(__y)) { }
334 
335 
336  template<typename _U1, typename _U2, typename
337  enable_if<_PCCFP<_U1, _U2>::template
338  _MoveConstructiblePair<_U1, _U2>()
340  _ImplicitlyMoveConvertiblePair<_U1, _U2>(),
341  bool>::type=true>
342  constexpr pair(pair<_U1, _U2>&& __p)
343  : first(std::forward<_U1>(__p.first)),
344  second(std::forward<_U2>(__p.second)) { }
345 
346  template<typename _U1, typename _U2, typename
347  enable_if<_PCCFP<_U1, _U2>::template
348  _MoveConstructiblePair<_U1, _U2>()
350  _ImplicitlyMoveConvertiblePair<_U1, _U2>(),
351  bool>::type=false>
352  explicit constexpr pair(pair<_U1, _U2>&& __p)
353  : first(std::forward<_U1>(__p.first)),
354  second(std::forward<_U2>(__p.second)) { }
355 
356  template<typename... _Args1, typename... _Args2>
358 
359  pair&
360  operator=(typename conditional<
361  __and_<is_copy_assignable<_T1>,
362  is_copy_assignable<_T2>>::value,
363  const pair&, const __nonesuch&>::type __p)
364  {
365  first = __p.first;
366  second = __p.second;
367  return *this;
368  }
369 
370  pair&
371  operator=(typename conditional<
372  __not_<__and_<is_copy_assignable<_T1>,
373  is_copy_assignable<_T2>>>::value,
374  const pair&, const __nonesuch&>::type __p) = delete;
375 
376  pair&
377  operator=(typename conditional<
378  __and_<is_move_assignable<_T1>,
379  is_move_assignable<_T2>>::value,
380  pair&&, __nonesuch&&>::type __p)
381  noexcept(__and_<is_nothrow_move_assignable<_T1>,
382  is_nothrow_move_assignable<_T2>>::value)
383  {
384  first = std::forward<first_type>(__p.first);
385  second = std::forward<second_type>(__p.second);
386  return *this;
387  }
388 
389  template<typename _U1, typename _U2>
390  typename enable_if<__and_<is_assignable<_T1&, const _U1&>,
391  is_assignable<_T2&, const _U2&>>::value,
392  pair&>::type
393  operator=(const pair<_U1, _U2>& __p)
394  {
395  first = __p.first;
396  second = __p.second;
397  return *this;
398  }
399 
400  template<typename _U1, typename _U2>
401  typename enable_if<__and_<is_assignable<_T1&, _U1&&>,
402  is_assignable<_T2&, _U2&&>>::value,
403  pair&>::type
404  operator=(pair<_U1, _U2>&& __p)
405  {
406  first = std::forward<_U1>(__p.first);
407  second = std::forward<_U2>(__p.second);
408  return *this;
409  }
410 
411  void
412  swap(pair& __p)
413  noexcept(__and_<__is_nothrow_swappable<_T1>,
414  __is_nothrow_swappable<_T2>>::value)
415  {
416  using std::swap;
417  swap(first, __p.first);
418  swap(second, __p.second);
419  }
420 
421  private:
422  template<typename... _Args1, std::size_t... _Indexes1,
423  typename... _Args2, std::size_t... _Indexes2>
425  _Index_tuple<_Indexes1...>, _Index_tuple<_Indexes2...>);
426 #endif
427  };
428 
429 #if __cpp_deduction_guides >= 201606
430  template<typename _T1, typename _T2> pair(_T1, _T2) -> pair<_T1, _T2>;
431 #endif
432 
433  /// Two pairs of the same type are equal iff their members are equal.
434  template<typename _T1, typename _T2>
435  inline _GLIBCXX_CONSTEXPR bool
436  operator==(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
437  { return __x.first == __y.first && __x.second == __y.second; }
438 
439  /// <http://gcc.gnu.org/onlinedocs/libstdc++/manual/utilities.html>
440  template<typename _T1, typename _T2>
441  inline _GLIBCXX_CONSTEXPR bool
442  operator<(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
443  { return __x.first < __y.first
444  || (!(__y.first < __x.first) && __x.second < __y.second); }
445 
446  /// Uses @c operator== to find the result.
447  template<typename _T1, typename _T2>
448  inline _GLIBCXX_CONSTEXPR bool
449  operator!=(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
450  { return !(__x == __y); }
451 
452  /// Uses @c operator< to find the result.
453  template<typename _T1, typename _T2>
454  inline _GLIBCXX_CONSTEXPR bool
455  operator>(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
456  { return __y < __x; }
457 
458  /// Uses @c operator< to find the result.
459  template<typename _T1, typename _T2>
460  inline _GLIBCXX_CONSTEXPR bool
461  operator<=(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
462  { return !(__y < __x); }
463 
464  /// Uses @c operator< to find the result.
465  template<typename _T1, typename _T2>
466  inline _GLIBCXX_CONSTEXPR bool
467  operator>=(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
468  { return !(__x < __y); }
469 
470 #if __cplusplus >= 201103L
471  /// See std::pair::swap().
472  // Note: no std::swap overloads in C++03 mode, this has performance
473  // implications, see, eg, libstdc++/38466.
474  template<typename _T1, typename _T2>
475  inline
476 #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
477  // Constrained free swap overload, see p0185r1
478  typename enable_if<__and_<__is_swappable<_T1>,
479  __is_swappable<_T2>>::value>::type
480 #else
481  void
482 #endif
484  noexcept(noexcept(__x.swap(__y)))
485  { __x.swap(__y); }
486 
487 #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
488  template<typename _T1, typename _T2>
489  typename enable_if<!__and_<__is_swappable<_T1>,
490  __is_swappable<_T2>>::value>::type
491  swap(pair<_T1, _T2>&, pair<_T1, _T2>&) = delete;
492 #endif
493 #endif // __cplusplus >= 201103L
494 
495  /**
496  * @brief A convenience wrapper for creating a pair from two objects.
497  * @param __x The first object.
498  * @param __y The second object.
499  * @return A newly-constructed pair<> object of the appropriate type.
500  *
501  * The standard requires that the objects be passed by reference-to-const,
502  * but LWG issue #181 says they should be passed by const value. We follow
503  * the LWG by default.
504  */
505  // _GLIBCXX_RESOLVE_LIB_DEFECTS
506  // 181. make_pair() unintended behavior
507 #if __cplusplus >= 201103L
508  // NB: DR 706.
509  template<typename _T1, typename _T2>
511  typename __decay_and_strip<_T2>::__type>
512  make_pair(_T1&& __x, _T2&& __y)
513  {
514  typedef typename __decay_and_strip<_T1>::__type __ds_type1;
515  typedef typename __decay_and_strip<_T2>::__type __ds_type2;
516  typedef pair<__ds_type1, __ds_type2> __pair_type;
517  return __pair_type(std::forward<_T1>(__x), std::forward<_T2>(__y));
518  }
519 #else
520  template<typename _T1, typename _T2>
521  inline pair<_T1, _T2>
522  make_pair(_T1 __x, _T2 __y)
523  { return pair<_T1, _T2>(__x, __y); }
524 #endif
525 
526  /// @}
527 
528 _GLIBCXX_END_NAMESPACE_VERSION
529 } // namespace std
530 
531 #endif /* _STL_PAIR_H */
_GLIBCXX17_INLINE constexpr piecewise_construct_t piecewise_construct
piecewise_construct
Definition: stl_pair.h:79
_T2 second
first is a copy of the first object
Definition: stl_pair.h:197
Primary class template, tuple.
Definition: tuple:53
ISO C++ entities toplevel namespace is std.
constexpr pair< typename __decay_and_strip< _T1 >::__type, typename __decay_and_strip< _T2 >::__type > make_pair(_T1 &&__x, _T2 &&__y)
A convenience wrapper for creating a pair from two objects.
Definition: stl_pair.h:512
_PCC< true, _BiIter, _BiIter > _PCCP
Definition: stl_pair.h:234
constexpr pair()
second is a copy of the second object
Definition: stl_pair.h:211
_T1 first
second_type is the second bound type
Definition: stl_pair.h:196
_T2 second_type
first_type is the first bound type
Definition: stl_pair.h:194
Struct holding two objects of arbitrary type.
Definition: stl_pair.h:191
_PCC<!is_same< _BiIter, _U1 >::value||!is_same< _BiIter, _U2 >::value, _BiIter, _BiIter > _PCCFP
Definition: stl_pair.h:265
piecewise_construct_t
Definition: stl_pair.h:76