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