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       constexpr pair(pair&&) = default;
00121 
00122       // DR 811.
00123       template<class _U1, class = typename
00124            enable_if<is_convertible<_U1, _T1>::value>::type>
00125     constexpr pair(_U1&& __x, const _T2& __y)
00126     : first(std::forward<_U1>(__x)), second(__y) { }
00127 
00128       template<class _U2, class = typename
00129            enable_if<is_convertible<_U2, _T2>::value>::type>
00130     constexpr pair(const _T1& __x, _U2&& __y)
00131     : first(__x), second(std::forward<_U2>(__y)) { }
00132 
00133       template<class _U1, class _U2, class = typename
00134            enable_if<__and_<is_convertible<_U1, _T1>,
00135                 is_convertible<_U2, _T2>>::value>::type>
00136     constexpr pair(_U1&& __x, _U2&& __y)
00137     : first(std::forward<_U1>(__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(pair<_U1, _U2>&& __p)
00143     : first(std::forward<_U1>(__p.first)),
00144       second(std::forward<_U2>(__p.second)) { }
00145 
00146       template<typename... _Args1, typename... _Args2>
00147         pair(piecewise_construct_t, tuple<_Args1...>, tuple<_Args2...>);
00148 
00149       pair&
00150       operator=(const pair& __p)
00151       {
00152     first = __p.first;
00153     second = __p.second;
00154     return *this;
00155       }
00156 
00157       pair&
00158       operator=(pair&& __p)
00159       noexcept(__and_<is_nothrow_move_assignable<_T1>,
00160                   is_nothrow_move_assignable<_T2>>::value)
00161       {
00162     first = std::forward<first_type>(__p.first);
00163     second = std::forward<second_type>(__p.second);
00164     return *this;
00165       }
00166 
00167       template<class _U1, class _U2>
00168     pair&
00169     operator=(const pair<_U1, _U2>& __p)
00170     {
00171       first = __p.first;
00172       second = __p.second;
00173       return *this;
00174     }
00175 
00176       template<class _U1, class _U2>
00177     pair&
00178     operator=(pair<_U1, _U2>&& __p)
00179     {
00180       first = std::forward<_U1>(__p.first);
00181       second = std::forward<_U2>(__p.second);
00182       return *this;
00183     }
00184 
00185       void
00186       swap(pair& __p)
00187       noexcept(noexcept(swap(first, __p.first))
00188            && noexcept(swap(second, __p.second)))
00189       {
00190     using std::swap;
00191     swap(first, __p.first);
00192     swap(second, __p.second);
00193       }
00194 
00195     private:
00196       template<typename... _Args1, std::size_t... _Indexes1,
00197                typename... _Args2, std::size_t... _Indexes2>
00198         pair(tuple<_Args1...>&, tuple<_Args2...>&,
00199              _Index_tuple<_Indexes1...>, _Index_tuple<_Indexes2...>);
00200 #endif
00201     };
00202 
00203   /// Two pairs of the same type are equal iff their members are equal.
00204   template<class _T1, class _T2>
00205     inline _GLIBCXX_CONSTEXPR bool
00206     operator==(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
00207     { return __x.first == __y.first && __x.second == __y.second; }
00208 
00209   /// <http://gcc.gnu.org/onlinedocs/libstdc++/manual/utilities.html>
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
00214          || (!(__y.first < __x.first) && __x.second < __y.second); }
00215 
00216   /// Uses @c operator== to find the result.
00217   template<class _T1, class _T2>
00218     inline _GLIBCXX_CONSTEXPR bool
00219     operator!=(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
00220     { return !(__x == __y); }
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 __y < __x; }
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 !(__x < __y); }
00239 
00240 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00241   /// See std::pair::swap().
00242   // Note:  no std::swap overloads in C++03 mode, this has performance
00243   //        implications, see, eg, libstdc++/38466.
00244   template<class _T1, class _T2>
00245     inline void
00246     swap(pair<_T1, _T2>& __x, pair<_T1, _T2>& __y)
00247     noexcept(noexcept(__x.swap(__y)))
00248     { __x.swap(__y); }
00249 #endif
00250 
00251   /**
00252    *  @brief A convenience wrapper for creating a pair from two objects.
00253    *  @param  __x  The first object.
00254    *  @param  __y  The second object.
00255    *  @return   A newly-constructed pair<> object of the appropriate type.
00256    *
00257    *  The standard requires that the objects be passed by reference-to-const,
00258    *  but LWG issue #181 says they should be passed by const value.  We follow
00259    *  the LWG by default.
00260    */
00261   // _GLIBCXX_RESOLVE_LIB_DEFECTS
00262   // 181.  make_pair() unintended behavior
00263 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00264   // NB: DR 706.
00265   template<class _T1, class _T2>
00266     constexpr pair<typename __decay_and_strip<_T1>::__type,
00267                    typename __decay_and_strip<_T2>::__type>
00268     make_pair(_T1&& __x, _T2&& __y)
00269     {
00270       typedef typename __decay_and_strip<_T1>::__type __ds_type1;
00271       typedef typename __decay_and_strip<_T2>::__type __ds_type2;
00272       typedef pair<__ds_type1, __ds_type2>        __pair_type;
00273       return __pair_type(std::forward<_T1>(__x), std::forward<_T2>(__y));
00274     }
00275 #else
00276   template<class _T1, class _T2>
00277     inline pair<_T1, _T2>
00278     make_pair(_T1 __x, _T2 __y)
00279     { return pair<_T1, _T2>(__x, __y); }
00280 #endif
00281 
00282 _GLIBCXX_END_NAMESPACE_VERSION
00283 } // namespace
00284 
00285 #endif /* _STL_PAIR_H */