1*e4b17023SJohn Marino // Pair implementation -*- C++ -*-
2*e4b17023SJohn Marino
3*e4b17023SJohn Marino // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
4*e4b17023SJohn Marino // 2010, 2011, 2012
5*e4b17023SJohn Marino // Free Software Foundation, Inc.
6*e4b17023SJohn Marino //
7*e4b17023SJohn Marino // This file is part of the GNU ISO C++ Library. This library is free
8*e4b17023SJohn Marino // software; you can redistribute it and/or modify it under the
9*e4b17023SJohn Marino // terms of the GNU General Public License as published by the
10*e4b17023SJohn Marino // Free Software Foundation; either version 3, or (at your option)
11*e4b17023SJohn Marino // any later version.
12*e4b17023SJohn Marino
13*e4b17023SJohn Marino // This library is distributed in the hope that it will be useful,
14*e4b17023SJohn Marino // but WITHOUT ANY WARRANTY; without even the implied warranty of
15*e4b17023SJohn Marino // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16*e4b17023SJohn Marino // GNU General Public License for more details.
17*e4b17023SJohn Marino
18*e4b17023SJohn Marino // Under Section 7 of GPL version 3, you are granted additional
19*e4b17023SJohn Marino // permissions described in the GCC Runtime Library Exception, version
20*e4b17023SJohn Marino // 3.1, as published by the Free Software Foundation.
21*e4b17023SJohn Marino
22*e4b17023SJohn Marino // You should have received a copy of the GNU General Public License and
23*e4b17023SJohn Marino // a copy of the GCC Runtime Library Exception along with this program;
24*e4b17023SJohn Marino // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25*e4b17023SJohn Marino // <http://www.gnu.org/licenses/>.
26*e4b17023SJohn Marino
27*e4b17023SJohn Marino /*
28*e4b17023SJohn Marino *
29*e4b17023SJohn Marino * Copyright (c) 1994
30*e4b17023SJohn Marino * Hewlett-Packard Company
31*e4b17023SJohn Marino *
32*e4b17023SJohn Marino * Permission to use, copy, modify, distribute and sell this software
33*e4b17023SJohn Marino * and its documentation for any purpose is hereby granted without fee,
34*e4b17023SJohn Marino * provided that the above copyright notice appear in all copies and
35*e4b17023SJohn Marino * that both that copyright notice and this permission notice appear
36*e4b17023SJohn Marino * in supporting documentation. Hewlett-Packard Company makes no
37*e4b17023SJohn Marino * representations about the suitability of this software for any
38*e4b17023SJohn Marino * purpose. It is provided "as is" without express or implied warranty.
39*e4b17023SJohn Marino *
40*e4b17023SJohn Marino *
41*e4b17023SJohn Marino * Copyright (c) 1996,1997
42*e4b17023SJohn Marino * Silicon Graphics Computer Systems, Inc.
43*e4b17023SJohn Marino *
44*e4b17023SJohn Marino * Permission to use, copy, modify, distribute and sell this software
45*e4b17023SJohn Marino * and its documentation for any purpose is hereby granted without fee,
46*e4b17023SJohn Marino * provided that the above copyright notice appear in all copies and
47*e4b17023SJohn Marino * that both that copyright notice and this permission notice appear
48*e4b17023SJohn Marino * in supporting documentation. Silicon Graphics makes no
49*e4b17023SJohn Marino * representations about the suitability of this software for any
50*e4b17023SJohn Marino * purpose. It is provided "as is" without express or implied warranty.
51*e4b17023SJohn Marino */
52*e4b17023SJohn Marino
53*e4b17023SJohn Marino /** @file bits/stl_pair.h
54*e4b17023SJohn Marino * This is an internal header file, included by other library headers.
55*e4b17023SJohn Marino * Do not attempt to use it directly. @headername{utility}
56*e4b17023SJohn Marino */
57*e4b17023SJohn Marino
58*e4b17023SJohn Marino #ifndef _STL_PAIR_H
59*e4b17023SJohn Marino #define _STL_PAIR_H 1
60*e4b17023SJohn Marino
61*e4b17023SJohn Marino #include <bits/move.h> // for std::move / std::forward, and std::swap
62*e4b17023SJohn Marino
63*e4b17023SJohn Marino #ifdef __GXX_EXPERIMENTAL_CXX0X__
64*e4b17023SJohn Marino #include <type_traits> // for std::__decay_and_strip too
65*e4b17023SJohn Marino #endif
66*e4b17023SJohn Marino
_GLIBCXX_VISIBILITY(default)67*e4b17023SJohn Marino namespace std _GLIBCXX_VISIBILITY(default)
68*e4b17023SJohn Marino {
69*e4b17023SJohn Marino _GLIBCXX_BEGIN_NAMESPACE_VERSION
70*e4b17023SJohn Marino
71*e4b17023SJohn Marino #ifdef __GXX_EXPERIMENTAL_CXX0X__
72*e4b17023SJohn Marino /// piecewise_construct_t
73*e4b17023SJohn Marino struct piecewise_construct_t { };
74*e4b17023SJohn Marino
75*e4b17023SJohn Marino /// piecewise_construct
76*e4b17023SJohn Marino constexpr piecewise_construct_t piecewise_construct = piecewise_construct_t();
77*e4b17023SJohn Marino
78*e4b17023SJohn Marino // Forward declarations.
79*e4b17023SJohn Marino template<typename...>
80*e4b17023SJohn Marino class tuple;
81*e4b17023SJohn Marino
82*e4b17023SJohn Marino template<std::size_t...>
83*e4b17023SJohn Marino struct _Index_tuple;
84*e4b17023SJohn Marino #endif
85*e4b17023SJohn Marino
86*e4b17023SJohn Marino /// Struct holding two objects of arbitrary type.
87*e4b17023SJohn Marino template<class _T1, class _T2>
88*e4b17023SJohn Marino struct pair
89*e4b17023SJohn Marino {
90*e4b17023SJohn Marino typedef _T1 first_type; /// @c first_type is the first bound type
91*e4b17023SJohn Marino typedef _T2 second_type; /// @c second_type is the second bound type
92*e4b17023SJohn Marino
93*e4b17023SJohn Marino _T1 first; /// @c first is a copy of the first object
94*e4b17023SJohn Marino _T2 second; /// @c second is a copy of the second object
95*e4b17023SJohn Marino
96*e4b17023SJohn Marino // _GLIBCXX_RESOLVE_LIB_DEFECTS
97*e4b17023SJohn Marino // 265. std::pair::pair() effects overly restrictive
98*e4b17023SJohn Marino /** The default constructor creates @c first and @c second using their
99*e4b17023SJohn Marino * respective default constructors. */
100*e4b17023SJohn Marino _GLIBCXX_CONSTEXPR pair()
101*e4b17023SJohn Marino : first(), second() { }
102*e4b17023SJohn Marino
103*e4b17023SJohn Marino /** Two objects may be passed to a @c pair constructor to be copied. */
104*e4b17023SJohn Marino _GLIBCXX_CONSTEXPR pair(const _T1& __a, const _T2& __b)
105*e4b17023SJohn Marino : first(__a), second(__b) { }
106*e4b17023SJohn Marino
107*e4b17023SJohn Marino /** There is also a templated copy ctor for the @c pair class itself. */
108*e4b17023SJohn Marino #ifndef __GXX_EXPERIMENTAL_CXX0X__
109*e4b17023SJohn Marino template<class _U1, class _U2>
110*e4b17023SJohn Marino pair(const pair<_U1, _U2>& __p)
111*e4b17023SJohn Marino : first(__p.first), second(__p.second) { }
112*e4b17023SJohn Marino #else
113*e4b17023SJohn Marino template<class _U1, class _U2, class = typename
114*e4b17023SJohn Marino enable_if<__and_<is_convertible<const _U1&, _T1>,
115*e4b17023SJohn Marino is_convertible<const _U2&, _T2>>::value>::type>
116*e4b17023SJohn Marino constexpr pair(const pair<_U1, _U2>& __p)
117*e4b17023SJohn Marino : first(__p.first), second(__p.second) { }
118*e4b17023SJohn Marino
119*e4b17023SJohn Marino constexpr pair(const pair&) = default;
120*e4b17023SJohn Marino constexpr pair(pair&&) = default;
121*e4b17023SJohn Marino
122*e4b17023SJohn Marino // DR 811.
123*e4b17023SJohn Marino template<class _U1, class = typename
124*e4b17023SJohn Marino enable_if<is_convertible<_U1, _T1>::value>::type>
125*e4b17023SJohn Marino constexpr pair(_U1&& __x, const _T2& __y)
126*e4b17023SJohn Marino : first(std::forward<_U1>(__x)), second(__y) { }
127*e4b17023SJohn Marino
128*e4b17023SJohn Marino template<class _U2, class = typename
129*e4b17023SJohn Marino enable_if<is_convertible<_U2, _T2>::value>::type>
130*e4b17023SJohn Marino constexpr pair(const _T1& __x, _U2&& __y)
131*e4b17023SJohn Marino : first(__x), second(std::forward<_U2>(__y)) { }
132*e4b17023SJohn Marino
133*e4b17023SJohn Marino template<class _U1, class _U2, class = typename
134*e4b17023SJohn Marino enable_if<__and_<is_convertible<_U1, _T1>,
135*e4b17023SJohn Marino is_convertible<_U2, _T2>>::value>::type>
136*e4b17023SJohn Marino constexpr pair(_U1&& __x, _U2&& __y)
137*e4b17023SJohn Marino : first(std::forward<_U1>(__x)), second(std::forward<_U2>(__y)) { }
138*e4b17023SJohn Marino
139*e4b17023SJohn Marino template<class _U1, class _U2, class = typename
140*e4b17023SJohn Marino enable_if<__and_<is_convertible<_U1, _T1>,
141*e4b17023SJohn Marino is_convertible<_U2, _T2>>::value>::type>
142*e4b17023SJohn Marino constexpr pair(pair<_U1, _U2>&& __p)
143*e4b17023SJohn Marino : first(std::forward<_U1>(__p.first)),
144*e4b17023SJohn Marino second(std::forward<_U2>(__p.second)) { }
145*e4b17023SJohn Marino
146*e4b17023SJohn Marino template<typename... _Args1, typename... _Args2>
147*e4b17023SJohn Marino pair(piecewise_construct_t, tuple<_Args1...>, tuple<_Args2...>);
148*e4b17023SJohn Marino
149*e4b17023SJohn Marino pair&
150*e4b17023SJohn Marino operator=(const pair& __p)
151*e4b17023SJohn Marino {
152*e4b17023SJohn Marino first = __p.first;
153*e4b17023SJohn Marino second = __p.second;
154*e4b17023SJohn Marino return *this;
155*e4b17023SJohn Marino }
156*e4b17023SJohn Marino
157*e4b17023SJohn Marino pair&
158*e4b17023SJohn Marino operator=(pair&& __p)
159*e4b17023SJohn Marino noexcept(__and_<is_nothrow_move_assignable<_T1>,
160*e4b17023SJohn Marino is_nothrow_move_assignable<_T2>>::value)
161*e4b17023SJohn Marino {
162*e4b17023SJohn Marino first = std::forward<first_type>(__p.first);
163*e4b17023SJohn Marino second = std::forward<second_type>(__p.second);
164*e4b17023SJohn Marino return *this;
165*e4b17023SJohn Marino }
166*e4b17023SJohn Marino
167*e4b17023SJohn Marino template<class _U1, class _U2>
168*e4b17023SJohn Marino pair&
169*e4b17023SJohn Marino operator=(const pair<_U1, _U2>& __p)
170*e4b17023SJohn Marino {
171*e4b17023SJohn Marino first = __p.first;
172*e4b17023SJohn Marino second = __p.second;
173*e4b17023SJohn Marino return *this;
174*e4b17023SJohn Marino }
175*e4b17023SJohn Marino
176*e4b17023SJohn Marino template<class _U1, class _U2>
177*e4b17023SJohn Marino pair&
178*e4b17023SJohn Marino operator=(pair<_U1, _U2>&& __p)
179*e4b17023SJohn Marino {
180*e4b17023SJohn Marino first = std::forward<_U1>(__p.first);
181*e4b17023SJohn Marino second = std::forward<_U2>(__p.second);
182*e4b17023SJohn Marino return *this;
183*e4b17023SJohn Marino }
184*e4b17023SJohn Marino
185*e4b17023SJohn Marino void
186*e4b17023SJohn Marino swap(pair& __p)
187*e4b17023SJohn Marino noexcept(noexcept(swap(first, __p.first))
188*e4b17023SJohn Marino && noexcept(swap(second, __p.second)))
189*e4b17023SJohn Marino {
190*e4b17023SJohn Marino using std::swap;
191*e4b17023SJohn Marino swap(first, __p.first);
192*e4b17023SJohn Marino swap(second, __p.second);
193*e4b17023SJohn Marino }
194*e4b17023SJohn Marino
195*e4b17023SJohn Marino private:
196*e4b17023SJohn Marino template<typename... _Args1, std::size_t... _Indexes1,
197*e4b17023SJohn Marino typename... _Args2, std::size_t... _Indexes2>
198*e4b17023SJohn Marino pair(tuple<_Args1...>&, tuple<_Args2...>&,
199*e4b17023SJohn Marino _Index_tuple<_Indexes1...>, _Index_tuple<_Indexes2...>);
200*e4b17023SJohn Marino #endif
201*e4b17023SJohn Marino };
202*e4b17023SJohn Marino
203*e4b17023SJohn Marino /// Two pairs of the same type are equal iff their members are equal.
204*e4b17023SJohn Marino template<class _T1, class _T2>
205*e4b17023SJohn Marino inline _GLIBCXX_CONSTEXPR bool
206*e4b17023SJohn Marino operator==(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
207*e4b17023SJohn Marino { return __x.first == __y.first && __x.second == __y.second; }
208*e4b17023SJohn Marino
209*e4b17023SJohn Marino /// <http://gcc.gnu.org/onlinedocs/libstdc++/manual/utilities.html>
210*e4b17023SJohn Marino template<class _T1, class _T2>
211*e4b17023SJohn Marino inline _GLIBCXX_CONSTEXPR bool
212*e4b17023SJohn Marino operator<(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
213*e4b17023SJohn Marino { return __x.first < __y.first
214*e4b17023SJohn Marino || (!(__y.first < __x.first) && __x.second < __y.second); }
215*e4b17023SJohn Marino
216*e4b17023SJohn Marino /// Uses @c operator== to find the result.
217*e4b17023SJohn Marino template<class _T1, class _T2>
218*e4b17023SJohn Marino inline _GLIBCXX_CONSTEXPR bool
219*e4b17023SJohn Marino operator!=(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
220*e4b17023SJohn Marino { return !(__x == __y); }
221*e4b17023SJohn Marino
222*e4b17023SJohn Marino /// Uses @c operator< to find the result.
223*e4b17023SJohn Marino template<class _T1, class _T2>
224*e4b17023SJohn Marino inline _GLIBCXX_CONSTEXPR bool
225*e4b17023SJohn Marino operator>(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
226*e4b17023SJohn Marino { return __y < __x; }
227*e4b17023SJohn Marino
228*e4b17023SJohn Marino /// Uses @c operator< to find the result.
229*e4b17023SJohn Marino template<class _T1, class _T2>
230*e4b17023SJohn Marino inline _GLIBCXX_CONSTEXPR bool
231*e4b17023SJohn Marino operator<=(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
232*e4b17023SJohn Marino { return !(__y < __x); }
233*e4b17023SJohn Marino
234*e4b17023SJohn Marino /// Uses @c operator< to find the result.
235*e4b17023SJohn Marino template<class _T1, class _T2>
236*e4b17023SJohn Marino inline _GLIBCXX_CONSTEXPR bool
237*e4b17023SJohn Marino operator>=(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
238*e4b17023SJohn Marino { return !(__x < __y); }
239*e4b17023SJohn Marino
240*e4b17023SJohn Marino #ifdef __GXX_EXPERIMENTAL_CXX0X__
241*e4b17023SJohn Marino /// See std::pair::swap().
242*e4b17023SJohn Marino // Note: no std::swap overloads in C++03 mode, this has performance
243*e4b17023SJohn Marino // implications, see, eg, libstdc++/38466.
244*e4b17023SJohn Marino template<class _T1, class _T2>
245*e4b17023SJohn Marino inline void
246*e4b17023SJohn Marino swap(pair<_T1, _T2>& __x, pair<_T1, _T2>& __y)
247*e4b17023SJohn Marino noexcept(noexcept(__x.swap(__y)))
248*e4b17023SJohn Marino { __x.swap(__y); }
249*e4b17023SJohn Marino #endif
250*e4b17023SJohn Marino
251*e4b17023SJohn Marino /**
252*e4b17023SJohn Marino * @brief A convenience wrapper for creating a pair from two objects.
253*e4b17023SJohn Marino * @param __x The first object.
254*e4b17023SJohn Marino * @param __y The second object.
255*e4b17023SJohn Marino * @return A newly-constructed pair<> object of the appropriate type.
256*e4b17023SJohn Marino *
257*e4b17023SJohn Marino * The standard requires that the objects be passed by reference-to-const,
258*e4b17023SJohn Marino * but LWG issue #181 says they should be passed by const value. We follow
259*e4b17023SJohn Marino * the LWG by default.
260*e4b17023SJohn Marino */
261*e4b17023SJohn Marino // _GLIBCXX_RESOLVE_LIB_DEFECTS
262*e4b17023SJohn Marino // 181. make_pair() unintended behavior
263*e4b17023SJohn Marino #ifdef __GXX_EXPERIMENTAL_CXX0X__
264*e4b17023SJohn Marino // NB: DR 706.
265*e4b17023SJohn Marino template<class _T1, class _T2>
266*e4b17023SJohn Marino constexpr pair<typename __decay_and_strip<_T1>::__type,
267*e4b17023SJohn Marino typename __decay_and_strip<_T2>::__type>
268*e4b17023SJohn Marino make_pair(_T1&& __x, _T2&& __y)
269*e4b17023SJohn Marino {
270*e4b17023SJohn Marino typedef typename __decay_and_strip<_T1>::__type __ds_type1;
271*e4b17023SJohn Marino typedef typename __decay_and_strip<_T2>::__type __ds_type2;
272*e4b17023SJohn Marino typedef pair<__ds_type1, __ds_type2> __pair_type;
273*e4b17023SJohn Marino return __pair_type(std::forward<_T1>(__x), std::forward<_T2>(__y));
274*e4b17023SJohn Marino }
275*e4b17023SJohn Marino #else
276*e4b17023SJohn Marino template<class _T1, class _T2>
277*e4b17023SJohn Marino inline pair<_T1, _T2>
278*e4b17023SJohn Marino make_pair(_T1 __x, _T2 __y)
279*e4b17023SJohn Marino { return pair<_T1, _T2>(__x, __y); }
280*e4b17023SJohn Marino #endif
281*e4b17023SJohn Marino
282*e4b17023SJohn Marino _GLIBCXX_END_NAMESPACE_VERSION
283*e4b17023SJohn Marino } // namespace
284*e4b17023SJohn Marino
285*e4b17023SJohn Marino #endif /* _STL_PAIR_H */
286