Merge from mainline (165734:167278).
[official-gcc/graphite-test-results.git] / libstdc++-v3 / include / bits / stl_pair.h
blobc6753f6d5d9faff1b05a7e79db4d54ca9fafb065
1 // Pair implementation -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
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 3, 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 // Under Section 7 of GPL version 3, you are granted additional
18 // permissions described in the GCC Runtime Library Exception, version
19 // 3.1, as published by the Free Software Foundation.
21 // You should have received a copy of the GNU General Public License and
22 // a copy of the GCC Runtime Library Exception along with this program;
23 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 // <http://www.gnu.org/licenses/>.
28 * Copyright (c) 1994
29 * Hewlett-Packard Company
31 * Permission to use, copy, modify, distribute and sell this software
32 * and its documentation for any purpose is hereby granted without fee,
33 * provided that the above copyright notice appear in all copies and
34 * that both that copyright notice and this permission notice appear
35 * in supporting documentation. Hewlett-Packard Company makes no
36 * representations about the suitability of this software for any
37 * purpose. It is provided "as is" without express or implied warranty.
40 * Copyright (c) 1996,1997
41 * Silicon Graphics Computer Systems, Inc.
43 * Permission to use, copy, modify, distribute and sell this software
44 * and its documentation for any purpose is hereby granted without fee,
45 * provided that the above copyright notice appear in all copies and
46 * that both that copyright notice and this permission notice appear
47 * in supporting documentation. Silicon Graphics makes no
48 * representations about the suitability of this software for any
49 * purpose. It is provided "as is" without express or implied warranty.
52 /** @file stl_pair.h
53 * This is an internal header file, included by other library headers.
54 * You should not attempt to use it directly.
57 #ifndef _STL_PAIR_H
58 #define _STL_PAIR_H 1
60 #include <bits/move.h> // for std::move / std::forward, and std::swap
62 #ifdef __GXX_EXPERIMENTAL_CXX0X__
63 #include <type_traits> // for std::__decay_and_strip too
64 #endif
66 _GLIBCXX_BEGIN_NAMESPACE(std)
68 #ifdef __GXX_EXPERIMENTAL_CXX0X__
69 /// piecewise_construct_t
70 struct piecewise_construct_t { };
72 /// piecewise_construct
73 constexpr piecewise_construct_t piecewise_construct = piecewise_construct_t();
75 // forward declarations
76 template<typename...>
77 class tuple;
79 template<int...>
80 struct _Index_tuple;
81 #endif
83 /// pair holds two objects of arbitrary type.
84 template<class _T1, class _T2>
85 struct pair
87 typedef _T1 first_type; ///< @c first_type is the first bound type
88 typedef _T2 second_type; ///< @c second_type is the second bound type
90 _T1 first; ///< @c first is a copy of the first object
91 _T2 second; ///< @c second is a copy of the second object
93 // _GLIBCXX_RESOLVE_LIB_DEFECTS
94 // 265. std::pair::pair() effects overly restrictive
95 /** The default constructor creates @c first and @c second using their
96 * respective default constructors. */
97 _GLIBCXX_CONSTEXPR pair()
98 : first(), second() { }
100 /** Two objects may be passed to a @c pair constructor to be copied. */
101 _GLIBCXX_CONSTEXPR pair(const _T1& __a, const _T2& __b)
102 : first(__a), second(__b) { }
104 /** There is also a templated copy ctor for the @c pair class itself. */
105 template<class _U1, class _U2>
106 _GLIBCXX_CONSTEXPR pair(const pair<_U1, _U2>& __p)
107 : first(__p.first), second(__p.second) { }
109 #ifdef __GXX_EXPERIMENTAL_CXX0X__
110 constexpr pair(const pair&) = default;
112 // Implicit.
113 // pair(pair&&) = default;
115 // DR 811.
116 template<class _U1, class = typename
117 std::enable_if<std::is_convertible<_U1, _T1>::value>::type>
118 pair(_U1&& __x, const _T2& __y)
119 : first(std::forward<_U1>(__x)), second(__y) { }
121 template<class _U2, class = typename
122 std::enable_if<std::is_convertible<_U2, _T2>::value>::type>
123 pair(const _T1& __x, _U2&& __y)
124 : first(__x), second(std::forward<_U2>(__y)) { }
126 template<class _U1, class _U2, class = typename
127 std::enable_if<std::is_convertible<_U1, _T1>::value
128 && std::is_convertible<_U2, _T2>::value>::type>
129 pair(_U1&& __x, _U2&& __y)
130 : first(std::forward<_U1>(__x)), second(std::forward<_U2>(__y)) { }
132 template<class _U1, class _U2>
133 pair(pair<_U1, _U2>&& __p)
134 : first(std::forward<_U1>(__p.first)),
135 second(std::forward<_U2>(__p.second)) { }
137 template<class... _Args1, class... _Args2>
138 pair(piecewise_construct_t,
139 tuple<_Args1...> __first, tuple<_Args2...> __second)
140 : first(__cons<first_type>(std::move(__first))),
141 second(__cons<second_type>(std::move(__second))) { }
143 pair&
144 operator=(const pair& __p)
146 first = __p.first;
147 second = __p.second;
148 return *this;
151 pair&
152 operator=(pair&& __p)
154 first = std::move(__p.first);
155 second = std::move(__p.second);
156 return *this;
159 template<class _U1, class _U2>
160 pair&
161 operator=(const pair<_U1, _U2>& __p)
163 first = __p.first;
164 second = __p.second;
165 return *this;
168 template<class _U1, class _U2>
169 pair&
170 operator=(pair<_U1, _U2>&& __p)
172 first = std::move(__p.first);
173 second = std::move(__p.second);
174 return *this;
177 void
178 swap(pair& __p)
180 using std::swap;
181 swap(first, __p.first);
182 swap(second, __p.second);
185 private:
186 template<typename _Tp, typename... _Args>
187 static _Tp
188 __cons(tuple<_Args...>&&);
190 template<typename _Tp, typename... _Args, int... _Indexes>
191 static _Tp
192 __do_cons(tuple<_Args...>&&, const _Index_tuple<_Indexes...>&);
193 #endif
196 /// Two pairs of the same type are equal iff their members are equal.
197 template<class _T1, class _T2>
198 inline _GLIBCXX_CONSTEXPR bool
199 operator==(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
200 { return __x.first == __y.first && __x.second == __y.second; }
202 /// <http://gcc.gnu.org/onlinedocs/libstdc++/manual/utilities.html>
203 template<class _T1, class _T2>
204 inline _GLIBCXX_CONSTEXPR bool
205 operator<(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
206 { return __x.first < __y.first
207 || (!(__y.first < __x.first) && __x.second < __y.second); }
209 /// Uses @c operator== to find the result.
210 template<class _T1, class _T2>
211 inline _GLIBCXX_CONSTEXPR bool
212 operator!=(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
213 { return !(__x == __y); }
215 /// Uses @c operator< to find the result.
216 template<class _T1, class _T2>
217 inline _GLIBCXX_CONSTEXPR bool
218 operator>(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
219 { return __y < __x; }
221 /// Uses @c operator< to find the result.
222 template<class _T1, class _T2>
223 inline _GLIBCXX_CONSTEXPR bool
224 operator<=(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
225 { return !(__y < __x); }
227 /// Uses @c operator< to find the result.
228 template<class _T1, class _T2>
229 inline _GLIBCXX_CONSTEXPR bool
230 operator>=(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
231 { return !(__x < __y); }
233 #ifdef __GXX_EXPERIMENTAL_CXX0X__
234 /// See std::pair::swap().
235 // Note: no std::swap overloads in C++03 mode, this has performance
236 // implications, see, eg, libstdc++/38466.
237 template<class _T1, class _T2>
238 inline void
239 swap(pair<_T1, _T2>& __x, pair<_T1, _T2>& __y)
240 { __x.swap(__y); }
241 #endif
244 * @brief A convenience wrapper for creating a pair from two objects.
245 * @param x The first object.
246 * @param y The second object.
247 * @return A newly-constructed pair<> object of the appropriate type.
249 * The standard requires that the objects be passed by reference-to-const,
250 * but LWG issue #181 says they should be passed by const value. We follow
251 * the LWG by default.
253 // _GLIBCXX_RESOLVE_LIB_DEFECTS
254 // 181. make_pair() unintended behavior
255 #ifdef __GXX_EXPERIMENTAL_CXX0X__
256 // NB: DR 706.
257 template<class _T1, class _T2>
258 inline pair<typename __decay_and_strip<_T1>::__type,
259 typename __decay_and_strip<_T2>::__type>
260 make_pair(_T1&& __x, _T2&& __y)
262 typedef typename __decay_and_strip<_T1>::__type __ds_type1;
263 typedef typename __decay_and_strip<_T2>::__type __ds_type2;
264 typedef pair<__ds_type1, __ds_type2> __pair_type;
265 return __pair_type(std::forward<_T1>(__x), std::forward<_T2>(__y));
267 #else
268 template<class _T1, class _T2>
269 inline pair<_T1, _T2>
270 make_pair(_T1 __x, _T2 __y)
271 { return pair<_T1, _T2>(__x, __y); }
272 #endif
274 _GLIBCXX_END_NAMESPACE
276 #endif /* _STL_PAIR_H */