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