1// Pair implementation -*- C++ -*- 2 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. 11 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. 16 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. 20 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/>. 25 26/* 27 * 28 * Copyright (c) 1994 29 * Hewlett-Packard Company 30 * 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. 38 * 39 * 40 * Copyright (c) 1996,1997 41 * Silicon Graphics Computer Systems, Inc. 42 * 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. 50 */ 51 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. 55 */ 56 57#ifndef _STL_PAIR_H 58#define _STL_PAIR_H 1 59 60#include <bits/move.h> // for std::move / std::forward, std::decay, and 61 // std::swap 62 63#ifdef __GXX_EXPERIMENTAL_CXX0X__ 64#include <type_traits> 65#endif 66 67_GLIBCXX_BEGIN_NAMESPACE(std) 68 69 /// pair holds two objects of arbitrary type. 70 template<class _T1, class _T2> 71 struct pair 72 { 73 typedef _T1 first_type; ///< @c first_type is the first bound type 74 typedef _T2 second_type; ///< @c second_type is the second bound type 75 76 _T1 first; ///< @c first is a copy of the first object 77 _T2 second; ///< @c second is a copy of the second object 78 79 // _GLIBCXX_RESOLVE_LIB_DEFECTS 80 // 265. std::pair::pair() effects overly restrictive 81 /** The default constructor creates @c first and @c second using their 82 * respective default constructors. */ 83 pair() 84 : first(), second() { } 85 86 /** Two objects may be passed to a @c pair constructor to be copied. */ 87 pair(const _T1& __a, const _T2& __b) 88 : first(__a), second(__b) { } 89 90#ifdef __GXX_EXPERIMENTAL_CXX0X__ 91 // DR 811. 92 template<class _U1, class = typename 93 std::enable_if<std::is_convertible<_U1, _T1>::value>::type> 94 pair(_U1&& __x, const _T2& __y) 95 : first(std::forward<_U1>(__x)), 96 second(__y) { } 97 98 template<class _U2, class = typename 99 std::enable_if<std::is_convertible<_U2, _T2>::value>::type> 100 pair(const _T1& __x, _U2&& __y) 101 : first(__x), 102 second(std::forward<_U2>(__y)) { } 103 104 template<class _U1, class _U2, class = typename 105 std::enable_if<std::is_convertible<_U1, _T1>::value 106 && std::is_convertible<_U2, _T2>::value>::type> 107 pair(_U1&& __x, _U2&& __y) 108 : first(std::forward<_U1>(__x)), 109 second(std::forward<_U2>(__y)) { } 110#endif 111 112 /** There is also a templated copy ctor for the @c pair class itself. */ 113 template<class _U1, class _U2> 114 pair(const pair<_U1, _U2>& __p) 115 : first(__p.first), 116 second(__p.second) { } 117 118#ifdef __GXX_EXPERIMENTAL_CXX0X__ 119 template<class _U1, class _U2> 120 pair(pair<_U1, _U2>&& __p) 121 : first(std::forward<_U1>(__p.first)), 122 second(std::forward<_U2>(__p.second)) { } 123 124 pair& 125 operator=(pair&& __p) 126 { 127 first = std::move(__p.first); 128 second = std::move(__p.second); 129 return *this; 130 } 131 132 template<class _U1, class _U2> 133 pair& 134 operator=(pair<_U1, _U2>&& __p) 135 { 136 first = std::move(__p.first); 137 second = std::move(__p.second); 138 return *this; 139 } 140 141 void 142 swap(pair& __p) 143 { 144 using std::swap; 145 swap(first, __p.first); 146 swap(second, __p.second); 147 } 148#endif 149 }; 150 151 /// Two pairs of the same type are equal iff their members are equal. 152 template<class _T1, class _T2> 153 inline bool 154 operator==(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y) 155 { return __x.first == __y.first && __x.second == __y.second; } 156 157 /// <http://gcc.gnu.org/onlinedocs/libstdc++/manual/utilities.html> 158 template<class _T1, class _T2> 159 inline bool 160 operator<(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y) 161 { return __x.first < __y.first 162 || (!(__y.first < __x.first) && __x.second < __y.second); } 163 164 /// Uses @c operator== to find the result. 165 template<class _T1, class _T2> 166 inline bool 167 operator!=(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y) 168 { return !(__x == __y); } 169 170 /// Uses @c operator< to find the result. 171 template<class _T1, class _T2> 172 inline bool 173 operator>(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y) 174 { return __y < __x; } 175 176 /// Uses @c operator< to find the result. 177 template<class _T1, class _T2> 178 inline bool 179 operator<=(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y) 180 { return !(__y < __x); } 181 182 /// Uses @c operator< to find the result. 183 template<class _T1, class _T2> 184 inline bool 185 operator>=(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y) 186 { return !(__x < __y); } 187 188#ifdef __GXX_EXPERIMENTAL_CXX0X__ 189 /// See std::pair::swap(). 190 // Note: no std::swap overloads in C++03 mode, this has performance 191 // implications, see, eg, libstdc++/38466. 192 template<class _T1, class _T2> 193 inline void 194 swap(pair<_T1, _T2>& __x, pair<_T1, _T2>& __y) 195 { __x.swap(__y); } 196#endif 197 198 /** 199 * @brief A convenience wrapper for creating a pair from two objects. 200 * @param x The first object. 201 * @param y The second object. 202 * @return A newly-constructed pair<> object of the appropriate type. 203 * 204 * The standard requires that the objects be passed by reference-to-const, 205 * but LWG issue #181 says they should be passed by const value. We follow 206 * the LWG by default. 207 */ 208 // _GLIBCXX_RESOLVE_LIB_DEFECTS 209 // 181. make_pair() unintended behavior 210#ifndef __GXX_EXPERIMENTAL_CXX0X__ 211 template<class _T1, class _T2> 212 inline pair<_T1, _T2> 213 make_pair(_T1 __x, _T2 __y) 214 { return pair<_T1, _T2>(__x, __y); } 215#else 216 template<typename _Tp> 217 class reference_wrapper; 218 219 // Helper which adds a reference to a type when given a reference_wrapper 220 template<typename _Tp> 221 struct __strip_reference_wrapper 222 { 223 typedef _Tp __type; 224 }; 225 226 template<typename _Tp> 227 struct __strip_reference_wrapper<reference_wrapper<_Tp> > 228 { 229 typedef _Tp& __type; 230 }; 231 232 template<typename _Tp> 233 struct __strip_reference_wrapper<const reference_wrapper<_Tp> > 234 { 235 typedef _Tp& __type; 236 }; 237 238 template<typename _Tp> 239 struct __decay_and_strip 240 { 241 typedef typename __strip_reference_wrapper< 242 typename decay<_Tp>::type>::__type __type; 243 }; 244 245 // NB: DR 706. 246 template<class _T1, class _T2> 247 inline pair<typename __decay_and_strip<_T1>::__type, 248 typename __decay_and_strip<_T2>::__type> 249 make_pair(_T1&& __x, _T2&& __y) 250 { 251 return pair<typename __decay_and_strip<_T1>::__type, 252 typename __decay_and_strip<_T2>::__type> 253 (std::forward<_T1>(__x), std::forward<_T2>(__y)); 254 } 255#endif 256 257_GLIBCXX_END_NAMESPACE 258 259#endif /* _STL_PAIR_H */ 260