1 // Pair implementation -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007
4 // Free Software Foundation, Inc.
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 2, or (at your option)
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING. If not, write to the Free
19 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction. Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License. This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
34 * Hewlett-Packard Company
36 * Permission to use, copy, modify, distribute and sell this software
37 * and its documentation for any purpose is hereby granted without fee,
38 * provided that the above copyright notice appear in all copies and
39 * that both that copyright notice and this permission notice appear
40 * in supporting documentation. Hewlett-Packard Company makes no
41 * representations about the suitability of this software for any
42 * purpose. It is provided "as is" without express or implied warranty.
45 * Copyright (c) 1996,1997
46 * Silicon Graphics Computer Systems, Inc.
48 * Permission to use, copy, modify, distribute and sell this software
49 * and its documentation for any purpose is hereby granted without fee,
50 * provided that the above copyright notice appear in all copies and
51 * that both that copyright notice and this permission notice appear
52 * in supporting documentation. Silicon Graphics makes no
53 * representations about the suitability of this software for any
54 * purpose. It is provided "as is" without express or implied warranty.
58 * This is an internal header file, included by other library headers.
59 * You should not attempt to use it directly.
65 #include <bits/stl_move.h> // for std::move / std::forward, std::decay, and
68 _GLIBCXX_BEGIN_NAMESPACE(std)
70 /// pair holds two objects of arbitrary type.
71 template<class _T1, class _T2>
74 typedef _T1 first_type; ///< @c first_type is the first bound type
75 typedef _T2 second_type; ///< @c second_type is the second bound type
77 _T1 first; ///< @c first is a copy of the first object
78 _T2 second; ///< @c second is a copy of the second object
80 // _GLIBCXX_RESOLVE_LIB_DEFECTS
81 // 265. std::pair::pair() effects overly restrictive
82 /** The default constructor creates @c first and @c second using their
83 * respective default constructors. */
85 : first(), second() { }
87 /** Two objects may be passed to a @c pair constructor to be copied. */
88 pair(const _T1& __a, const _T2& __b)
89 : first(__a), second(__b) { }
91 #ifdef __GXX_EXPERIMENTAL_CXX0X__
92 template<class _U1, class _U2>
93 pair(_U1&& __x, _U2&& __y)
94 : first(std::forward<_U1>(__x)),
95 second(std::forward<_U2>(__y)) { }
98 : first(std::move(__p.first)),
99 second(std::move(__p.second)) { }
102 /** There is also a templated copy ctor for the @c pair class itself. */
103 template<class _U1, class _U2>
104 pair(const pair<_U1, _U2>& __p)
106 second(__p.second) { }
108 #ifdef __GXX_EXPERIMENTAL_CXX0X__
109 template<class _U1, class _U2>
110 pair(pair<_U1, _U2>&& __p)
111 : first(std::move(__p.first)),
112 second(std::move(__p.second)) { }
114 // http://gcc.gnu.org/ml/libstdc++/2007-08/msg00052.html
115 template<class _U1, class _Arg0, class... _Args>
116 pair(_U1&& __x, _Arg0&& __arg0, _Args&&... __args)
117 : first(std::forward<_U1>(__x)),
118 second(std::forward<_Arg0>(__arg0),
119 std::forward<_Args>(__args)...) { }
122 operator=(pair&& __p)
124 first = std::move(__p.first);
125 second = std::move(__p.second);
129 template<class _U1, class _U2>
131 operator=(pair<_U1, _U2>&& __p)
133 first = std::move(__p.first);
134 second = std::move(__p.second);
142 swap(first, __p.first);
143 swap(second, __p.second);
148 /// Two pairs of the same type are equal iff their members are equal.
149 template<class _T1, class _T2>
151 operator==(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
152 { return __x.first == __y.first && __x.second == __y.second; }
154 /// <http://gcc.gnu.org/onlinedocs/libstdc++/manual/utilities.html>
155 template<class _T1, class _T2>
157 operator<(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
158 { return __x.first < __y.first
159 || (!(__y.first < __x.first) && __x.second < __y.second); }
161 /// Uses @c operator== to find the result.
162 template<class _T1, class _T2>
164 operator!=(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
165 { return !(__x == __y); }
167 /// Uses @c operator< to find the result.
168 template<class _T1, class _T2>
170 operator>(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
171 { return __y < __x; }
173 /// Uses @c operator< to find the result.
174 template<class _T1, class _T2>
176 operator<=(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
177 { return !(__y < __x); }
179 /// Uses @c operator< to find the result.
180 template<class _T1, class _T2>
182 operator>=(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
183 { return !(__x < __y); }
185 #ifdef __GXX_EXPERIMENTAL_CXX0X__
186 /// See std::pair::swap().
187 template<class _T1, class _T2>
189 swap(pair<_T1, _T2>& __x, pair<_T1, _T2>& __y)
192 template<class _T1, class _T2>
194 swap(pair<_T1, _T2>&& __x, pair<_T1, _T2>& __y)
197 template<class _T1, class _T2>
199 swap(pair<_T1, _T2>& __x, pair<_T1, _T2>&& __y)
204 * @brief A convenience wrapper for creating a pair from two objects.
205 * @param x The first object.
206 * @param y The second object.
207 * @return A newly-constructed pair<> object of the appropriate type.
209 * The standard requires that the objects be passed by reference-to-const,
210 * but LWG issue #181 says they should be passed by const value. We follow
211 * the LWG by default.
213 // _GLIBCXX_RESOLVE_LIB_DEFECTS
214 // 181. make_pair() unintended behavior
215 #ifndef __GXX_EXPERIMENTAL_CXX0X__
216 template<class _T1, class _T2>
217 inline pair<_T1, _T2>
218 make_pair(_T1 __x, _T2 __y)
219 { return pair<_T1, _T2>(__x, __y); }
221 template<typename _Tp>
222 class reference_wrapper;
224 // Helper which adds a reference to a type when given a reference_wrapper
225 template<typename _Tp>
226 struct __strip_reference_wrapper
231 template<typename _Tp>
232 struct __strip_reference_wrapper<reference_wrapper<_Tp> >
237 template<typename _Tp>
238 struct __strip_reference_wrapper<const reference_wrapper<_Tp> >
243 template<typename _Tp>
244 struct __decay_and_strip
246 typedef typename __strip_reference_wrapper<
247 typename decay<_Tp>::type>::__type __type;
251 template<class _T1, class _T2>
252 inline pair<typename __decay_and_strip<_T1>::__type,
253 typename __decay_and_strip<_T2>::__type>
254 make_pair(_T1&& __x, _T2&& __y)
256 return pair<typename __decay_and_strip<_T1>::__type,
257 typename __decay_and_strip<_T2>::__type>
258 (std::forward<_T1>(__x), std::forward<_T2>(__y));
262 _GLIBCXX_END_NAMESPACE
264 #endif /* _STL_PAIR_H */