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