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