Merge -r 127928:132243 from trunk
[official-gcc.git] / libstdc++-v3 / include / bits / stl_pair.h
blob3bda5018058e51502d73de43cdb74e7b227b4003
1 // Pair implementation -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007
4 // Free Software Foundation, Inc.
5 //
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)
10 // any later version.
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,
20 // USA.
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.
33 * Copyright (c) 1994
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.
57 /** @file stl_pair.h
58 * This is an internal header file, included by other library headers.
59 * You should not attempt to use it directly.
62 #ifndef _STL_PAIR_H
63 #define _STL_PAIR_H 1
65 #include <bits/stl_move.h> // for std::move / std::forward, std::decay, and
66 // std::swap
68 _GLIBCXX_BEGIN_NAMESPACE(std)
70 /// pair holds two objects of arbitrary type.
71 template<class _T1, class _T2>
72 struct pair
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. */
84 pair()
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)) { }
97 pair(pair&& __p)
98 : first(std::move(__p.first)),
99 second(std::move(__p.second)) { }
100 #endif
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)
105 : first(__p.first),
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)...) { }
121 pair&
122 operator=(pair&& __p)
124 first = std::move(__p.first);
125 second = std::move(__p.second);
126 return *this;
129 template<class _U1, class _U2>
130 pair&
131 operator=(pair<_U1, _U2>&& __p)
133 first = std::move(__p.first);
134 second = std::move(__p.second);
135 return *this;
138 void
139 swap(pair&& __p)
141 using std::swap;
142 swap(first, __p.first);
143 swap(second, __p.second);
145 #endif
148 /// Two pairs of the same type are equal iff their members are equal.
149 template<class _T1, class _T2>
150 inline bool
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++/20_util/howto.html#pairlt>
155 template<class _T1, class _T2>
156 inline bool
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>
163 inline bool
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>
169 inline bool
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>
175 inline bool
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>
181 inline bool
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>
188 inline void
189 swap(pair<_T1, _T2>& __x, pair<_T1, _T2>& __y)
190 { __x.swap(__y); }
192 template<class _T1, class _T2>
193 inline void
194 swap(pair<_T1, _T2>&& __x, pair<_T1, _T2>& __y)
195 { __x.swap(__y); }
197 template<class _T1, class _T2>
198 inline void
199 swap(pair<_T1, _T2>& __x, pair<_T1, _T2>&& __y)
200 { __x.swap(__y); }
201 #endif
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); }
220 #else
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
228 typedef _Tp __type;
231 template<typename _Tp>
232 struct __strip_reference_wrapper<reference_wrapper<_Tp> >
234 typedef _Tp& __type;
237 template<typename _Tp>
238 struct __strip_reference_wrapper<const reference_wrapper<_Tp> >
240 typedef _Tp& __type;
243 template<typename _Tp>
244 struct __decay_and_strip
246 typedef typename __strip_reference_wrapper<
247 typename decay<_Tp>::type>::__type __type;
250 // NB: DR 706.
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));
260 #endif
262 _GLIBCXX_END_NAMESPACE
264 #endif /* _STL_PAIR_H */