|
libstdc++
|
00001 // Pair implementation -*- C++ -*- 00002 00003 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 00004 // 2010, 2011 00005 // Free Software Foundation, Inc. 00006 // 00007 // This file is part of the GNU ISO C++ Library. This library is free 00008 // software; you can redistribute it and/or modify it under the 00009 // terms of the GNU General Public License as published by the 00010 // Free Software Foundation; either version 3, or (at your option) 00011 // any later version. 00012 00013 // This library is distributed in the hope that it will be useful, 00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00016 // GNU General Public License for more details. 00017 00018 // Under Section 7 of GPL version 3, you are granted additional 00019 // permissions described in the GCC Runtime Library Exception, version 00020 // 3.1, as published by the Free Software Foundation. 00021 00022 // You should have received a copy of the GNU General Public License and 00023 // a copy of the GCC Runtime Library Exception along with this program; 00024 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 00025 // <http://www.gnu.org/licenses/>. 00026 00027 /* 00028 * 00029 * Copyright (c) 1994 00030 * Hewlett-Packard Company 00031 * 00032 * Permission to use, copy, modify, distribute and sell this software 00033 * and its documentation for any purpose is hereby granted without fee, 00034 * provided that the above copyright notice appear in all copies and 00035 * that both that copyright notice and this permission notice appear 00036 * in supporting documentation. Hewlett-Packard Company makes no 00037 * representations about the suitability of this software for any 00038 * purpose. It is provided "as is" without express or implied warranty. 00039 * 00040 * 00041 * Copyright (c) 1996,1997 00042 * Silicon Graphics Computer Systems, Inc. 00043 * 00044 * Permission to use, copy, modify, distribute and sell this software 00045 * and its documentation for any purpose is hereby granted without fee, 00046 * provided that the above copyright notice appear in all copies and 00047 * that both that copyright notice and this permission notice appear 00048 * in supporting documentation. Silicon Graphics makes no 00049 * representations about the suitability of this software for any 00050 * purpose. It is provided "as is" without express or implied warranty. 00051 */ 00052 00053 /** @file bits/stl_pair.h 00054 * This is an internal header file, included by other library headers. 00055 * Do not attempt to use it directly. @headername{utility} 00056 */ 00057 00058 #ifndef _STL_PAIR_H 00059 #define _STL_PAIR_H 1 00060 00061 #include <bits/move.h> // for std::move / std::forward, and std::swap 00062 00063 #ifdef __GXX_EXPERIMENTAL_CXX0X__ 00064 #include <type_traits> // for std::__decay_and_strip too 00065 #endif 00066 00067 namespace std _GLIBCXX_VISIBILITY(default) 00068 { 00069 _GLIBCXX_BEGIN_NAMESPACE_VERSION 00070 00071 #ifdef __GXX_EXPERIMENTAL_CXX0X__ 00072 /// piecewise_construct_t 00073 struct piecewise_construct_t { }; 00074 00075 /// piecewise_construct 00076 constexpr piecewise_construct_t piecewise_construct = piecewise_construct_t(); 00077 00078 // Forward declarations. 00079 template<typename...> 00080 class tuple; 00081 00082 template<std::size_t...> 00083 struct _Index_tuple; 00084 #endif 00085 00086 /** 00087 * @brief Struct holding two objects of arbitrary type. 00088 * 00089 * @tparam _T1 Type of first object. 00090 * @tparam _T2 Type of second object. 00091 */ 00092 template<class _T1, class _T2> 00093 struct pair 00094 { 00095 typedef _T1 first_type; /// @c first_type is the first bound type 00096 typedef _T2 second_type; /// @c second_type is the second bound type 00097 00098 _T1 first; /// @c first is a copy of the first object 00099 _T2 second; /// @c second is a copy of the second object 00100 00101 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00102 // 265. std::pair::pair() effects overly restrictive 00103 /** The default constructor creates @c first and @c second using their 00104 * respective default constructors. */ 00105 _GLIBCXX_CONSTEXPR pair() 00106 : first(), second() { } 00107 00108 /** Two objects may be passed to a @c pair constructor to be copied. */ 00109 _GLIBCXX_CONSTEXPR pair(const _T1& __a, const _T2& __b) 00110 : first(__a), second(__b) { } 00111 00112 /** There is also a templated copy ctor for the @c pair class itself. */ 00113 #ifndef __GXX_EXPERIMENTAL_CXX0X__ 00114 template<class _U1, class _U2> 00115 pair(const pair<_U1, _U2>& __p) 00116 : first(__p.first), second(__p.second) { } 00117 #else 00118 template<class _U1, class _U2, class = typename 00119 enable_if<__and_<is_convertible<const _U1&, _T1>, 00120 is_convertible<const _U2&, _T2>>::value>::type> 00121 constexpr pair(const pair<_U1, _U2>& __p) 00122 : first(__p.first), second(__p.second) { } 00123 00124 constexpr pair(const pair&) = default; 00125 00126 // XXX Defaulted?!? Breaks std::map!!! 00127 pair(pair&& __p) 00128 noexcept(__and_<is_nothrow_move_constructible<_T1>, 00129 is_nothrow_move_constructible<_T2>>::value) 00130 : first(std::forward<first_type>(__p.first)), 00131 second(std::forward<second_type>(__p.second)) { } 00132 00133 // DR 811. 00134 template<class _U1, class = typename 00135 enable_if<is_convertible<_U1, _T1>::value>::type> 00136 constexpr pair(_U1&& __x, const _T2& __y) 00137 : first(std::forward<_U1>(__x)), second(__y) { } 00138 00139 template<class _U2, class = typename 00140 enable_if<is_convertible<_U2, _T2>::value>::type> 00141 constexpr pair(const _T1& __x, _U2&& __y) 00142 : first(__x), second(std::forward<_U2>(__y)) { } 00143 00144 template<class _U1, class _U2, class = typename 00145 enable_if<__and_<is_convertible<_U1, _T1>, 00146 is_convertible<_U2, _T2>>::value>::type> 00147 constexpr pair(_U1&& __x, _U2&& __y) 00148 : first(std::forward<_U1>(__x)), second(std::forward<_U2>(__y)) { } 00149 00150 template<class _U1, class _U2, class = typename 00151 enable_if<__and_<is_convertible<_U1, _T1>, 00152 is_convertible<_U2, _T2>>::value>::type> 00153 constexpr pair(pair<_U1, _U2>&& __p) 00154 : first(std::forward<_U1>(__p.first)), 00155 second(std::forward<_U2>(__p.second)) { } 00156 00157 template<typename... _Args1, typename... _Args2> 00158 pair(piecewise_construct_t, tuple<_Args1...>, tuple<_Args2...>); 00159 00160 pair& 00161 operator=(const pair& __p) 00162 { 00163 first = __p.first; 00164 second = __p.second; 00165 return *this; 00166 } 00167 00168 pair& 00169 operator=(pair&& __p) 00170 noexcept(__and_<is_nothrow_move_assignable<_T1>, 00171 is_nothrow_move_assignable<_T2>>::value) 00172 { 00173 first = std::forward<first_type>(__p.first); 00174 second = std::forward<second_type>(__p.second); 00175 return *this; 00176 } 00177 00178 template<class _U1, class _U2> 00179 pair& 00180 operator=(const pair<_U1, _U2>& __p) 00181 { 00182 first = __p.first; 00183 second = __p.second; 00184 return *this; 00185 } 00186 00187 template<class _U1, class _U2> 00188 pair& 00189 operator=(pair<_U1, _U2>&& __p) 00190 { 00191 first = std::forward<_U1>(__p.first); 00192 second = std::forward<_U2>(__p.second); 00193 return *this; 00194 } 00195 00196 void 00197 swap(pair& __p) 00198 noexcept(noexcept(swap(first, __p.first)) 00199 && noexcept(swap(second, __p.second))) 00200 { 00201 using std::swap; 00202 swap(first, __p.first); 00203 swap(second, __p.second); 00204 } 00205 00206 private: 00207 template<typename... _Args1, std::size_t... _Indexes1, 00208 typename... _Args2, std::size_t... _Indexes2> 00209 pair(tuple<_Args1...>&, tuple<_Args2...>&, 00210 _Index_tuple<_Indexes1...>, _Index_tuple<_Indexes2...>); 00211 #endif 00212 }; 00213 00214 /// Two pairs of the same type are equal iff their members are equal. 00215 template<class _T1, class _T2> 00216 inline _GLIBCXX_CONSTEXPR bool 00217 operator==(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y) 00218 { return __x.first == __y.first && __x.second == __y.second; } 00219 00220 /// <http://gcc.gnu.org/onlinedocs/libstdc++/manual/utilities.html> 00221 template<class _T1, class _T2> 00222 inline _GLIBCXX_CONSTEXPR bool 00223 operator<(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y) 00224 { return __x.first < __y.first 00225 || (!(__y.first < __x.first) && __x.second < __y.second); } 00226 00227 /// Uses @c operator== to find the result. 00228 template<class _T1, class _T2> 00229 inline _GLIBCXX_CONSTEXPR bool 00230 operator!=(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y) 00231 { return !(__x == __y); } 00232 00233 /// Uses @c operator< to find the result. 00234 template<class _T1, class _T2> 00235 inline _GLIBCXX_CONSTEXPR bool 00236 operator>(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y) 00237 { return __y < __x; } 00238 00239 /// Uses @c operator< to find the result. 00240 template<class _T1, class _T2> 00241 inline _GLIBCXX_CONSTEXPR bool 00242 operator<=(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y) 00243 { return !(__y < __x); } 00244 00245 /// Uses @c operator< to find the result. 00246 template<class _T1, class _T2> 00247 inline _GLIBCXX_CONSTEXPR bool 00248 operator>=(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y) 00249 { return !(__x < __y); } 00250 00251 #ifdef __GXX_EXPERIMENTAL_CXX0X__ 00252 /// See std::pair::swap(). 00253 // Note: no std::swap overloads in C++03 mode, this has performance 00254 // implications, see, eg, libstdc++/38466. 00255 template<class _T1, class _T2> 00256 inline void 00257 swap(pair<_T1, _T2>& __x, pair<_T1, _T2>& __y) 00258 noexcept(noexcept(__x.swap(__y))) 00259 { __x.swap(__y); } 00260 #endif 00261 00262 /** 00263 * @brief A convenience wrapper for creating a pair from two objects. 00264 * @param __x The first object. 00265 * @param __y The second object. 00266 * @return A newly-constructed pair<> object of the appropriate type. 00267 * 00268 * The standard requires that the objects be passed by reference-to-const, 00269 * but LWG issue #181 says they should be passed by const value. We follow 00270 * the LWG by default. 00271 */ 00272 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00273 // 181. make_pair() unintended behavior 00274 #ifdef __GXX_EXPERIMENTAL_CXX0X__ 00275 // NB: DR 706. 00276 template<class _T1, class _T2> 00277 constexpr pair<typename __decay_and_strip<_T1>::__type, 00278 typename __decay_and_strip<_T2>::__type> 00279 make_pair(_T1&& __x, _T2&& __y) 00280 { 00281 typedef typename __decay_and_strip<_T1>::__type __ds_type1; 00282 typedef typename __decay_and_strip<_T2>::__type __ds_type2; 00283 typedef pair<__ds_type1, __ds_type2> __pair_type; 00284 return __pair_type(std::forward<_T1>(__x), std::forward<_T2>(__y)); 00285 } 00286 #else 00287 template<class _T1, class _T2> 00288 inline pair<_T1, _T2> 00289 make_pair(_T1 __x, _T2 __y) 00290 { return pair<_T1, _T2>(__x, __y); } 00291 #endif 00292 00293 _GLIBCXX_END_NAMESPACE_VERSION 00294 } // namespace 00295 00296 #endif /* _STL_PAIR_H */