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
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 2, 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 // You should have received a copy of the GNU General Public License along
00018 // with this library; see the file COPYING.  If not, write to the Free
00019 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
00020 // USA.
00021 
00022 // As a special exception, you may use this file as part of a free software
00023 // library without restriction.  Specifically, if other files instantiate
00024 // templates or use macros or inline functions from this file, or you compile
00025 // this file and link it with other files to produce an executable, this
00026 // file does not by itself cause the resulting executable to be covered by
00027 // the GNU General Public License.  This exception does not however
00028 // invalidate any other reasons why the executable file might be covered by
00029 // the GNU General Public License.
00030 
00031 /*
00032  *
00033  * Copyright (c) 1994
00034  * Hewlett-Packard Company
00035  *
00036  * Permission to use, copy, modify, distribute and sell this software
00037  * and its documentation for any purpose is hereby granted without fee,
00038  * provided that the above copyright notice appear in all copies and
00039  * that both that copyright notice and this permission notice appear
00040  * in supporting documentation.  Hewlett-Packard Company makes no
00041  * representations about the suitability of this software for any
00042  * purpose.  It is provided "as is" without express or implied warranty.
00043  *
00044  *
00045  * Copyright (c) 1996,1997
00046  * Silicon Graphics Computer Systems, Inc.
00047  *
00048  * Permission to use, copy, modify, distribute and sell this software
00049  * and its documentation for any purpose is hereby granted without fee,
00050  * provided that the above copyright notice appear in all copies and
00051  * that both that copyright notice and this permission notice appear
00052  * in supporting documentation.  Silicon Graphics makes no
00053  * representations about the suitability of this software for any
00054  * purpose.  It is provided "as is" without express or implied warranty.
00055  */
00056 
00057 /** @file stl_pair.h
00058  *  This is an internal header file, included by other library headers.
00059  *  You should not attempt to use it directly.
00060  */
00061 
00062 #ifndef _STL_PAIR_H
00063 #define _STL_PAIR_H 1
00064 
00065 #include <bits/stl_move.h> // for std::move / std::forward, std::decay, and
00066                            // std::swap
00067 
00068 _GLIBCXX_BEGIN_NAMESPACE(std)
00069 
00070   /// pair holds two objects of arbitrary type.
00071   template<class _T1, class _T2>
00072     struct pair
00073     {
00074       typedef _T1 first_type;    ///<  @c first_type is the first bound type
00075       typedef _T2 second_type;   ///<  @c second_type is the second bound type
00076 
00077       _T1 first;                 ///< @c first is a copy of the first object
00078       _T2 second;                ///< @c second is a copy of the second object
00079 
00080       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00081       // 265.  std::pair::pair() effects overly restrictive
00082       /** The default constructor creates @c first and @c second using their
00083        *  respective default constructors.  */
00084       pair()
00085       : first(), second() { }
00086 
00087       /** Two objects may be passed to a @c pair constructor to be copied.  */
00088       pair(const _T1& __a, const _T2& __b)
00089       : first(__a), second(__b) { }
00090 
00091 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00092       template<class _U1, class _U2>
00093         pair(_U1&& __x, _U2&& __y)
00094     : first(std::forward<_U1>(__x)),
00095       second(std::forward<_U2>(__y)) { }
00096 
00097       pair(pair&& __p)
00098       : first(std::move(__p.first)),
00099     second(std::move(__p.second)) { }
00100 #endif
00101 
00102       /** There is also a templated copy ctor for the @c pair class itself.  */
00103       template<class _U1, class _U2>
00104         pair(const pair<_U1, _U2>& __p)
00105     : first(__p.first),
00106       second(__p.second) { }
00107 
00108 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00109       template<class _U1, class _U2>
00110         pair(pair<_U1, _U2>&& __p)
00111     : first(std::move(__p.first)),
00112       second(std::move(__p.second)) { }
00113 
00114       // http://gcc.gnu.org/ml/libstdc++/2007-08/msg00052.html
00115       template<class _U1, class _Arg0, class... _Args>
00116         pair(_U1&& __x, _Arg0&& __arg0, _Args&&... __args)
00117     : first(std::forward<_U1>(__x)),
00118       second(std::forward<_Arg0>(__arg0),
00119          std::forward<_Args>(__args)...) { }
00120 
00121       pair&
00122       operator=(pair&& __p)
00123       { 
00124     first = std::move(__p.first);
00125     second = std::move(__p.second);
00126     return *this;
00127       }
00128 
00129       template<class _U1, class _U2>
00130         pair&
00131         operator=(pair<_U1, _U2>&& __p)
00132     {
00133       first = std::move(__p.first);
00134       second = std::move(__p.second);
00135       return *this;
00136     }
00137 
00138       void
00139       swap(pair&& __p)
00140       {
00141     using std::swap;
00142     swap(first, __p.first);
00143     swap(second, __p.second);   
00144       }
00145 #endif
00146     };
00147 
00148   /// Two pairs of the same type are equal iff their members are equal.
00149   template<class _T1, class _T2>
00150     inline bool
00151     operator==(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
00152     { return __x.first == __y.first && __x.second == __y.second; }
00153 
00154   /// <http://gcc.gnu.org/onlinedocs/libstdc++/20_util/howto.html#pairlt>
00155   template<class _T1, class _T2>
00156     inline bool
00157     operator<(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
00158     { return __x.first < __y.first
00159          || (!(__y.first < __x.first) && __x.second < __y.second); }
00160 
00161   /// Uses @c operator== to find the result.
00162   template<class _T1, class _T2>
00163     inline bool
00164     operator!=(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
00165     { return !(__x == __y); }
00166 
00167   /// Uses @c operator< to find the result.
00168   template<class _T1, class _T2>
00169     inline bool
00170     operator>(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
00171     { return __y < __x; }
00172 
00173   /// Uses @c operator< to find the result.
00174   template<class _T1, class _T2>
00175     inline bool
00176     operator<=(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
00177     { return !(__y < __x); }
00178 
00179   /// Uses @c operator< to find the result.
00180   template<class _T1, class _T2>
00181     inline bool
00182     operator>=(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
00183     { return !(__x < __y); }
00184 
00185 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00186   /// See std::pair::swap().
00187   template<class _T1, class _T2>
00188     inline void
00189     swap(pair<_T1, _T2>& __x, pair<_T1, _T2>& __y)
00190     { __x.swap(__y); }
00191 
00192   template<class _T1, class _T2>
00193     inline void
00194     swap(pair<_T1, _T2>&& __x, pair<_T1, _T2>& __y)
00195     { __x.swap(__y); }
00196 
00197   template<class _T1, class _T2>
00198     inline void
00199     swap(pair<_T1, _T2>& __x, pair<_T1, _T2>&& __y)
00200     { __x.swap(__y); }
00201 #endif
00202 
00203   /**
00204    *  @brief A convenience wrapper for creating a pair from two objects.
00205    *  @param  x  The first object.
00206    *  @param  y  The second object.
00207    *  @return   A newly-constructed pair<> object of the appropriate type.
00208    *
00209    *  The standard requires that the objects be passed by reference-to-const,
00210    *  but LWG issue #181 says they should be passed by const value.  We follow
00211    *  the LWG by default.
00212    */
00213   // _GLIBCXX_RESOLVE_LIB_DEFECTS
00214   // 181.  make_pair() unintended behavior
00215 #ifndef __GXX_EXPERIMENTAL_CXX0X__
00216   template<class _T1, class _T2>
00217     inline pair<_T1, _T2>
00218     make_pair(_T1 __x, _T2 __y)
00219     { return pair<_T1, _T2>(__x, __y); }
00220 #else
00221   template<typename _Tp>
00222     class reference_wrapper;
00223 
00224   // Helper which adds a reference to a type when given a reference_wrapper
00225   template<typename _Tp>
00226     struct __strip_reference_wrapper
00227     {
00228       typedef _Tp __type;
00229     };
00230 
00231   template<typename _Tp>
00232     struct __strip_reference_wrapper<reference_wrapper<_Tp> >
00233     {
00234       typedef _Tp& __type;
00235     };
00236 
00237   template<typename _Tp>
00238     struct __strip_reference_wrapper<const reference_wrapper<_Tp> >
00239     {
00240       typedef _Tp& __type;
00241     };
00242 
00243   template<typename _Tp>
00244     struct __decay_and_strip
00245     {
00246       typedef typename __strip_reference_wrapper<
00247     typename decay<_Tp>::type>::__type __type;
00248     };
00249 
00250   // NB: DR 706.
00251   template<class _T1, class _T2>
00252     inline pair<typename __decay_and_strip<_T1>::__type,
00253         typename __decay_and_strip<_T2>::__type>
00254     make_pair(_T1&& __x, _T2&& __y)
00255     {
00256       return pair<typename __decay_and_strip<_T1>::__type,
00257               typename __decay_and_strip<_T2>::__type>
00258     (std::forward<_T1>(__x), std::forward<_T2>(__y));
00259     }
00260 #endif
00261 
00262 _GLIBCXX_END_NAMESPACE
00263 
00264 #endif /* _STL_PAIR_H */

Generated on Wed Mar 26 00:43:15 2008 for libstdc++ by  doxygen 1.5.1