libstdc++
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 // 2010, 2011
00005 // Free Software Foundation, Inc.
00006 //
00007 // This file is part of the GNU ISO C++ Library.  This library is free
00008 // software; you can redistribute it and/or modify it under the
00009 // terms of the GNU General Public License as published by the
00010 // Free Software Foundation; either version 3, or (at your option)
00011 // any later version.
00012 
00013 // This library is distributed in the hope that it will be useful,
00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 // GNU General Public License for more details.
00017 
00018 // Under Section 7 of GPL version 3, you are granted additional
00019 // permissions described in the GCC Runtime Library Exception, version
00020 // 3.1, as published by the Free Software Foundation.
00021 
00022 // You should have received a copy of the GNU General Public License and
00023 // a copy of the GCC Runtime Library Exception along with this program;
00024 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00025 // <http://www.gnu.org/licenses/>.
00026 
00027 /*
00028  *
00029  * Copyright (c) 1994
00030  * Hewlett-Packard Company
00031  *
00032  * Permission to use, copy, modify, distribute and sell this software
00033  * and its documentation for any purpose is hereby granted without fee,
00034  * provided that the above copyright notice appear in all copies and
00035  * that both that copyright notice and this permission notice appear
00036  * in supporting documentation.  Hewlett-Packard Company makes no
00037  * representations about the suitability of this software for any
00038  * purpose.  It is provided "as is" without express or implied warranty.
00039  *
00040  *
00041  * Copyright (c) 1996,1997
00042  * Silicon Graphics Computer Systems, Inc.
00043  *
00044  * Permission to use, copy, modify, distribute and sell this software
00045  * and its documentation for any purpose is hereby granted without fee,
00046  * provided that the above copyright notice appear in all copies and
00047  * that both that copyright notice and this permission notice appear
00048  * in supporting documentation.  Silicon Graphics makes no
00049  * representations about the suitability of this software for any
00050  * purpose.  It is provided "as is" without express or implied warranty.
00051  */
00052 
00053 /** @file bits/stl_pair.h
00054  *  This is an internal header file, included by other library headers.
00055  *  Do not attempt to use it directly. @headername{utility}
00056  */
00057 
00058 #ifndef _STL_PAIR_H
00059 #define _STL_PAIR_H 1
00060 
00061 #include <bits/move.h> // for std::move / std::forward, and std::swap
00062 
00063 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00064 #include <type_traits> // for std::__decay_and_strip too
00065 #endif
00066 
00067 namespace std _GLIBCXX_VISIBILITY(default)
00068 {
00069 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00070 
00071 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00072   /// piecewise_construct_t
00073   struct piecewise_construct_t { };
00074 
00075   /// piecewise_construct
00076   constexpr piecewise_construct_t piecewise_construct = piecewise_construct_t();
00077 
00078   // Forward declarations.
00079   template<typename...>
00080     class tuple;
00081 
00082   template<std::size_t...>
00083     struct _Index_tuple;
00084 #endif
00085 
00086   /// Struct holding two objects of arbitrary type.
00087   template<class _T1, class _T2>
00088     struct pair
00089     {
00090       typedef _T1 first_type;    /// @c first_type is the first bound type
00091       typedef _T2 second_type;   /// @c second_type is the second bound type
00092 
00093       _T1 first;                 /// @c first is a copy of the first object
00094       _T2 second;                /// @c second is a copy of the second object
00095 
00096       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00097       // 265.  std::pair::pair() effects overly restrictive
00098       /** The default constructor creates @c first and @c second using their
00099        *  respective default constructors.  */
00100       _GLIBCXX_CONSTEXPR pair()
00101       : first(), second() { }
00102 
00103       /** Two objects may be passed to a @c pair constructor to be copied.  */
00104       _GLIBCXX_CONSTEXPR pair(const _T1& __a, const _T2& __b)
00105       : first(__a), second(__b) { }
00106 
00107       /** There is also a templated copy ctor for the @c pair class itself.  */
00108 #ifndef __GXX_EXPERIMENTAL_CXX0X__
00109       template<class _U1, class _U2>
00110     pair(const pair<_U1, _U2>& __p)
00111     : first(__p.first), second(__p.second) { }
00112 #else
00113       template<class _U1, class _U2, class = typename
00114            enable_if<__and_<is_convertible<const _U1&, _T1>,
00115                 is_convertible<const _U2&, _T2>>::value>::type>
00116     constexpr pair(const pair<_U1, _U2>& __p)
00117     : first(__p.first), second(__p.second) { }
00118 
00119       constexpr pair(const pair&) = default;
00120 
00121       // XXX Defaulted?!? Breaks std::map!!!
00122       pair(pair&& __p)
00123       noexcept(__and_<is_nothrow_move_constructible<_T1>,
00124                   is_nothrow_move_constructible<_T2>>::value)
00125       : first(std::forward<first_type>(__p.first)),
00126     second(std::forward<second_type>(__p.second)) { }
00127 
00128       // DR 811.
00129       template<class _U1, class = typename
00130            enable_if<is_convertible<_U1, _T1>::value>::type>
00131     constexpr pair(_U1&& __x, const _T2& __y)
00132     : first(std::forward<_U1>(__x)), second(__y) { }
00133 
00134       template<class _U2, class = typename
00135            enable_if<is_convertible<_U2, _T2>::value>::type>
00136     constexpr pair(const _T1& __x, _U2&& __y)
00137     : first(__x), second(std::forward<_U2>(__y)) { }
00138 
00139       template<class _U1, class _U2, class = typename
00140            enable_if<__and_<is_convertible<_U1, _T1>,
00141                 is_convertible<_U2, _T2>>::value>::type>
00142     constexpr pair(_U1&& __x, _U2&& __y)
00143     : first(std::forward<_U1>(__x)), second(std::forward<_U2>(__y)) { }
00144 
00145       template<class _U1, class _U2, class = typename
00146            enable_if<__and_<is_convertible<_U1, _T1>,
00147                 is_convertible<_U2, _T2>>::value>::type>
00148     constexpr pair(pair<_U1, _U2>&& __p)
00149     : first(std::forward<_U1>(__p.first)),
00150       second(std::forward<_U2>(__p.second)) { }
00151 
00152       template<typename... _Args1, typename... _Args2>
00153         pair(piecewise_construct_t, tuple<_Args1...>, tuple<_Args2...>);
00154 
00155       pair&
00156       operator=(const pair& __p)
00157       {
00158     first = __p.first;
00159     second = __p.second;
00160     return *this;
00161       }
00162 
00163       pair&
00164       operator=(pair&& __p)
00165       noexcept(__and_<is_nothrow_move_assignable<_T1>,
00166                   is_nothrow_move_assignable<_T2>>::value)
00167       {
00168     first = std::forward<first_type>(__p.first);
00169     second = std::forward<second_type>(__p.second);
00170     return *this;
00171       }
00172 
00173       template<class _U1, class _U2>
00174     pair&
00175     operator=(const pair<_U1, _U2>& __p)
00176     {
00177       first = __p.first;
00178       second = __p.second;
00179       return *this;
00180     }
00181 
00182       template<class _U1, class _U2>
00183     pair&
00184     operator=(pair<_U1, _U2>&& __p)
00185     {
00186       first = std::forward<_U1>(__p.first);
00187       second = std::forward<_U2>(__p.second);
00188       return *this;
00189     }
00190 
00191       void
00192       swap(pair& __p)
00193       noexcept(noexcept(swap(first, __p.first))
00194            && noexcept(swap(second, __p.second)))
00195       {
00196     using std::swap;
00197     swap(first, __p.first);
00198     swap(second, __p.second);
00199       }
00200 
00201     private:
00202       template<typename... _Args1, std::size_t... _Indexes1,
00203                typename... _Args2, std::size_t... _Indexes2>
00204         pair(tuple<_Args1...>&, tuple<_Args2...>&,
00205              _Index_tuple<_Indexes1...>, _Index_tuple<_Indexes2...>);
00206 #endif
00207     };
00208 
00209   /// Two pairs of the same type are equal iff their members are equal.
00210   template<class _T1, class _T2>
00211     inline _GLIBCXX_CONSTEXPR bool
00212     operator==(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
00213     { return __x.first == __y.first && __x.second == __y.second; }
00214 
00215   /// <http://gcc.gnu.org/onlinedocs/libstdc++/manual/utilities.html>
00216   template<class _T1, class _T2>
00217     inline _GLIBCXX_CONSTEXPR bool
00218     operator<(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
00219     { return __x.first < __y.first
00220          || (!(__y.first < __x.first) && __x.second < __y.second); }
00221 
00222   /// Uses @c operator== to find the result.
00223   template<class _T1, class _T2>
00224     inline _GLIBCXX_CONSTEXPR bool
00225     operator!=(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
00226     { return !(__x == __y); }
00227 
00228   /// Uses @c operator< to find the result.
00229   template<class _T1, class _T2>
00230     inline _GLIBCXX_CONSTEXPR bool
00231     operator>(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
00232     { return __y < __x; }
00233 
00234   /// Uses @c operator< to find the result.
00235   template<class _T1, class _T2>
00236     inline _GLIBCXX_CONSTEXPR bool
00237     operator<=(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
00238     { return !(__y < __x); }
00239 
00240   /// Uses @c operator< to find the result.
00241   template<class _T1, class _T2>
00242     inline _GLIBCXX_CONSTEXPR bool
00243     operator>=(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
00244     { return !(__x < __y); }
00245 
00246 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00247   /// See std::pair::swap().
00248   // Note:  no std::swap overloads in C++03 mode, this has performance
00249   //        implications, see, eg, libstdc++/38466.
00250   template<class _T1, class _T2>
00251     inline void
00252     swap(pair<_T1, _T2>& __x, pair<_T1, _T2>& __y)
00253     noexcept(noexcept(__x.swap(__y)))
00254     { __x.swap(__y); }
00255 #endif
00256 
00257   /**
00258    *  @brief A convenience wrapper for creating a pair from two objects.
00259    *  @param  __x  The first object.
00260    *  @param  __y  The second object.
00261    *  @return   A newly-constructed pair<> object of the appropriate type.
00262    *
00263    *  The standard requires that the objects be passed by reference-to-const,
00264    *  but LWG issue #181 says they should be passed by const value.  We follow
00265    *  the LWG by default.
00266    */
00267   // _GLIBCXX_RESOLVE_LIB_DEFECTS
00268   // 181.  make_pair() unintended behavior
00269 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00270   // NB: DR 706.
00271   template<class _T1, class _T2>
00272     constexpr pair<typename __decay_and_strip<_T1>::__type,
00273                    typename __decay_and_strip<_T2>::__type>
00274     make_pair(_T1&& __x, _T2&& __y)
00275     {
00276       typedef typename __decay_and_strip<_T1>::__type __ds_type1;
00277       typedef typename __decay_and_strip<_T2>::__type __ds_type2;
00278       typedef pair<__ds_type1, __ds_type2>        __pair_type;
00279       return __pair_type(std::forward<_T1>(__x), std::forward<_T2>(__y));
00280     }
00281 #else
00282   template<class _T1, class _T2>
00283     inline pair<_T1, _T2>
00284     make_pair(_T1 __x, _T2 __y)
00285     { return pair<_T1, _T2>(__x, __y); }
00286 #endif
00287 
00288 _GLIBCXX_END_NAMESPACE_VERSION
00289 } // namespace
00290 
00291 #endif /* _STL_PAIR_H */