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