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 stl_pair.h
00053  *  This is an internal header file, included by other library headers.
00054  *  You should not attempt to use it directly.
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 _GLIBCXX_BEGIN_NAMESPACE(std)
00067 
00068 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00069   struct piecewise_construct_t { };
00070 
00071   static const piecewise_construct_t piecewise_construct
00072     = piecewise_construct_t();
00073 
00074   // forward declarations
00075   template<typename...>
00076     class tuple;
00077 
00078   template<int...>
00079     struct _Index_tuple;
00080 #endif
00081 
00082   /// pair holds two objects of arbitrary type.
00083   template<class _T1, class _T2>
00084     struct pair
00085     {
00086       typedef _T1 first_type;    ///<  @c first_type is the first bound type
00087       typedef _T2 second_type;   ///<  @c second_type is the second bound type
00088 
00089       _T1 first;                 ///< @c first is a copy of the first object
00090       _T2 second;                ///< @c second is a copy of the second object
00091 
00092       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00093       // 265.  std::pair::pair() effects overly restrictive
00094       /** The default constructor creates @c first and @c second using their
00095        *  respective default constructors.  */
00096       pair()
00097       : first(), second() { }
00098 
00099       /** Two objects may be passed to a @c pair constructor to be copied.  */
00100       pair(const _T1& __a, const _T2& __b)
00101       : first(__a), second(__b) { }
00102 
00103 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00104       pair(const pair&) = default;
00105 
00106       // DR 811.
00107       template<class _U1, class = typename
00108            std::enable_if<std::is_convertible<_U1, _T1>::value>::type>
00109         pair(_U1&& __x, const _T2& __y)
00110     : first(std::forward<_U1>(__x)),
00111       second(__y) { }
00112 
00113       template<class _U2, class = typename
00114            std::enable_if<std::is_convertible<_U2, _T2>::value>::type>
00115         pair(const _T1& __x, _U2&& __y)
00116     : first(__x),
00117       second(std::forward<_U2>(__y)) { }
00118 
00119       template<class _U1, class _U2, class = typename
00120            std::enable_if<std::is_convertible<_U1, _T1>::value
00121                   && std::is_convertible<_U2, _T2>::value>::type>
00122         pair(_U1&& __x, _U2&& __y)
00123     : first(std::forward<_U1>(__x)),
00124       second(std::forward<_U2>(__y)) { }
00125 
00126       template<class... _Args1, class... _Args2>
00127         pair(piecewise_construct_t,
00128          tuple<_Args1...> __first_args,
00129          tuple<_Args2...> __second_args)
00130     : first(__cons<first_type>(std::move(__first_args))),
00131       second(__cons<second_type>(std::move(__second_args))) { }
00132 #endif
00133 
00134       /** There is also a templated copy ctor for the @c pair class itself.  */
00135       template<class _U1, class _U2>
00136         pair(const pair<_U1, _U2>& __p)
00137     : first(__p.first),
00138       second(__p.second) { }
00139 
00140 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00141       template<class _U1, class _U2>
00142         pair(pair<_U1, _U2>&& __p)
00143     : first(std::forward<_U1>(__p.first)),
00144       second(std::forward<_U2>(__p.second)) { }
00145 
00146       pair&
00147       operator=(pair&& __p)
00148       { 
00149     first = std::move(__p.first);
00150     second = std::move(__p.second);
00151     return *this;
00152       }
00153 
00154       template<class _U1, class _U2>
00155         pair&
00156         operator=(const pair<_U1, _U2>& __p)
00157     {
00158       first = __p.first;
00159       second = __p.second;
00160       return *this;
00161     }
00162 
00163       template<class _U1, class _U2>
00164         pair&
00165         operator=(pair<_U1, _U2>&& __p)
00166     {
00167       first = std::move(__p.first);
00168       second = std::move(__p.second);
00169       return *this;
00170     }
00171 
00172       void
00173       swap(pair& __p)
00174       {
00175     using std::swap;
00176     swap(first, __p.first);
00177     swap(second, __p.second);   
00178       }
00179 
00180     private:
00181       template<typename _Tp, typename... _Args>
00182         static _Tp
00183         __cons(tuple<_Args...>&&);
00184 
00185       template<typename _Tp, typename... _Args, int... _Indexes>
00186         static _Tp
00187         __do_cons(tuple<_Args...>&&, const _Index_tuple<_Indexes...>&);
00188 #endif
00189     };
00190 
00191   /// Two pairs of the same type are equal iff their members are equal.
00192   template<class _T1, class _T2>
00193     inline bool
00194     operator==(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
00195     { return __x.first == __y.first && __x.second == __y.second; }
00196 
00197   /// <http://gcc.gnu.org/onlinedocs/libstdc++/manual/utilities.html>
00198   template<class _T1, class _T2>
00199     inline bool
00200     operator<(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
00201     { return __x.first < __y.first
00202          || (!(__y.first < __x.first) && __x.second < __y.second); }
00203 
00204   /// Uses @c operator== to find the result.
00205   template<class _T1, class _T2>
00206     inline bool
00207     operator!=(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
00208     { return !(__x == __y); }
00209 
00210   /// Uses @c operator< to find the result.
00211   template<class _T1, class _T2>
00212     inline bool
00213     operator>(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
00214     { return __y < __x; }
00215 
00216   /// Uses @c operator< to find the result.
00217   template<class _T1, class _T2>
00218     inline bool
00219     operator<=(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
00220     { return !(__y < __x); }
00221 
00222   /// Uses @c operator< to find the result.
00223   template<class _T1, class _T2>
00224     inline bool
00225     operator>=(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
00226     { return !(__x < __y); }
00227 
00228 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00229   /// See std::pair::swap().
00230   // Note:  no std::swap overloads in C++03 mode, this has performance
00231   //        implications, see, eg, libstdc++/38466.
00232   template<class _T1, class _T2>
00233     inline void
00234     swap(pair<_T1, _T2>& __x, pair<_T1, _T2>& __y)
00235     { __x.swap(__y); }
00236 #endif
00237 
00238   /**
00239    *  @brief A convenience wrapper for creating a pair from two objects.
00240    *  @param  x  The first object.
00241    *  @param  y  The second object.
00242    *  @return   A newly-constructed pair<> object of the appropriate type.
00243    *
00244    *  The standard requires that the objects be passed by reference-to-const,
00245    *  but LWG issue #181 says they should be passed by const value.  We follow
00246    *  the LWG by default.
00247    */
00248   // _GLIBCXX_RESOLVE_LIB_DEFECTS
00249   // 181.  make_pair() unintended behavior
00250 #ifndef __GXX_EXPERIMENTAL_CXX0X__
00251   template<class _T1, class _T2>
00252     inline pair<_T1, _T2>
00253     make_pair(_T1 __x, _T2 __y)
00254     { return pair<_T1, _T2>(__x, __y); }
00255 #else
00256   // NB: DR 706.
00257   template<class _T1, class _T2>
00258     inline pair<typename __decay_and_strip<_T1>::__type,
00259         typename __decay_and_strip<_T2>::__type>
00260     make_pair(_T1&& __x, _T2&& __y)
00261     {
00262       return pair<typename __decay_and_strip<_T1>::__type,
00263               typename __decay_and_strip<_T2>::__type>
00264     (std::forward<_T1>(__x), std::forward<_T2>(__y));
00265     }
00266 #endif
00267 
00268 _GLIBCXX_END_NAMESPACE
00269 
00270 #endif /* _STL_PAIR_H */