libstdc++
utility
Go to the documentation of this file.
00001 // <utility> -*- C++ -*-
00002 
00003 // Copyright (C) 2001-2018 Free Software Foundation, Inc.
00004 //
00005 // This file is part of the GNU ISO C++ Library.  This library is free
00006 // software; you can redistribute it and/or modify it under the
00007 // terms of the GNU General Public License as published by the
00008 // Free Software Foundation; either version 3, or (at your option)
00009 // any later version.
00010 
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 
00016 // Under Section 7 of GPL version 3, you are granted additional
00017 // permissions described in the GCC Runtime Library Exception, version
00018 // 3.1, as published by the Free Software Foundation.
00019 
00020 // You should have received a copy of the GNU General Public License and
00021 // a copy of the GCC Runtime Library Exception along with this program;
00022 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00023 // <http://www.gnu.org/licenses/>.
00024 
00025 /*
00026  *
00027  * Copyright (c) 1994
00028  * Hewlett-Packard Company
00029  *
00030  * Permission to use, copy, modify, distribute and sell this software
00031  * and its documentation for any purpose is hereby granted without fee,
00032  * provided that the above copyright notice appear in all copies and
00033  * that both that copyright notice and this permission notice appear
00034  * in supporting documentation.  Hewlett-Packard Company makes no
00035  * representations about the suitability of this software for any
00036  * purpose.  It is provided "as is" without express or implied warranty.
00037  *
00038  *
00039  * Copyright (c) 1996,1997
00040  * Silicon Graphics Computer Systems, Inc.
00041  *
00042  * Permission to use, copy, modify, distribute and sell this software
00043  * and its documentation for any purpose is hereby granted without fee,
00044  * provided that the above copyright notice appear in all copies and
00045  * that both that copyright notice and this permission notice appear
00046  * in supporting documentation.  Silicon Graphics makes no
00047  * representations about the suitability of this software for any
00048  * purpose.  It is provided "as is" without express or implied warranty.
00049  */
00050 
00051 /** @file include/utility
00052  *  This is a Standard C++ Library header.
00053  */
00054 
00055 #ifndef _GLIBCXX_UTILITY
00056 #define _GLIBCXX_UTILITY 1
00057 
00058 #pragma GCC system_header
00059 
00060 /**
00061  * @defgroup utilities Utilities
00062  *
00063  * Components deemed generally useful. Includes pair, tuple,
00064  * forward/move helpers, ratio, function object, metaprogramming and
00065  * type traits, time, date, and memory functions.
00066  */
00067 
00068 #include <bits/c++config.h>
00069 #include <bits/stl_relops.h>
00070 #include <bits/stl_pair.h>
00071 
00072 #if __cplusplus >= 201103L
00073 
00074 #include <type_traits>
00075 #include <bits/move.h>
00076 #include <initializer_list>
00077 
00078 namespace std _GLIBCXX_VISIBILITY(default)
00079 {
00080 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00081 
00082   /// Finds the size of a given tuple type.
00083   template<typename _Tp>
00084     struct tuple_size;
00085 
00086   // _GLIBCXX_RESOLVE_LIB_DEFECTS
00087   // 2313. tuple_size should always derive from integral_constant<size_t, N>
00088   // 2770. tuple_size<const T> specialization is not SFINAE compatible
00089 
00090   template<typename _Tp,
00091            typename _Up = typename remove_cv<_Tp>::type,
00092            typename = typename enable_if<is_same<_Tp, _Up>::value>::type,
00093            size_t = tuple_size<_Tp>::value>
00094     using __enable_if_has_tuple_size = _Tp;
00095 
00096   template<typename _Tp>
00097     struct tuple_size<const __enable_if_has_tuple_size<_Tp>>
00098     : public tuple_size<_Tp> { };
00099 
00100   template<typename _Tp>
00101     struct tuple_size<volatile __enable_if_has_tuple_size<_Tp>>
00102     : public tuple_size<_Tp> { };
00103 
00104   template<typename _Tp>
00105     struct tuple_size<const volatile __enable_if_has_tuple_size<_Tp>>
00106     : public tuple_size<_Tp> { };
00107 
00108   /// Gives the type of the ith element of a given tuple type.
00109   template<std::size_t __i, typename _Tp>
00110     struct tuple_element;
00111 
00112   // Duplicate of C++14's tuple_element_t for internal use in C++11 mode
00113   template<std::size_t __i, typename _Tp>
00114     using __tuple_element_t = typename tuple_element<__i, _Tp>::type;
00115 
00116   template<std::size_t __i, typename _Tp>
00117     struct tuple_element<__i, const _Tp>
00118     {
00119       typedef typename add_const<__tuple_element_t<__i, _Tp>>::type type;
00120     };
00121 
00122   template<std::size_t __i, typename _Tp>
00123     struct tuple_element<__i, volatile _Tp>
00124     {
00125       typedef typename add_volatile<__tuple_element_t<__i, _Tp>>::type type;
00126     };
00127 
00128   template<std::size_t __i, typename _Tp>
00129     struct tuple_element<__i, const volatile _Tp>
00130     {
00131       typedef typename add_cv<__tuple_element_t<__i, _Tp>>::type type;
00132     };
00133 
00134 #if __cplusplus > 201103L
00135 #define __cpp_lib_tuple_element_t 201402
00136 
00137   template<std::size_t __i, typename _Tp>
00138     using tuple_element_t = typename tuple_element<__i, _Tp>::type;
00139 #endif
00140 
00141   // Various functions which give std::pair a tuple-like interface.
00142 
00143   /// Partial specialization for std::pair
00144   template<typename _T1, typename _T2>
00145     struct __is_tuple_like_impl<std::pair<_T1, _T2>> : true_type
00146     { };
00147 
00148   /// Partial specialization for std::pair
00149   template<class _Tp1, class _Tp2>
00150     struct tuple_size<std::pair<_Tp1, _Tp2>>
00151     : public integral_constant<std::size_t, 2> { };
00152 
00153   /// Partial specialization for std::pair
00154   template<class _Tp1, class _Tp2>
00155     struct tuple_element<0, std::pair<_Tp1, _Tp2>>
00156     { typedef _Tp1 type; };
00157 
00158   /// Partial specialization for std::pair
00159   template<class _Tp1, class _Tp2>
00160     struct tuple_element<1, std::pair<_Tp1, _Tp2>>
00161     { typedef _Tp2 type; };
00162 
00163   template<std::size_t _Int>
00164     struct __pair_get;
00165 
00166   template<>
00167     struct __pair_get<0>
00168     {
00169       template<typename _Tp1, typename _Tp2>
00170         static constexpr _Tp1&
00171         __get(std::pair<_Tp1, _Tp2>& __pair) noexcept
00172         { return __pair.first; }
00173 
00174       template<typename _Tp1, typename _Tp2>
00175         static constexpr _Tp1&&
00176         __move_get(std::pair<_Tp1, _Tp2>&& __pair) noexcept
00177         { return std::forward<_Tp1>(__pair.first); }
00178 
00179       template<typename _Tp1, typename _Tp2>
00180         static constexpr const _Tp1&
00181         __const_get(const std::pair<_Tp1, _Tp2>& __pair) noexcept
00182         { return __pair.first; }
00183 
00184       template<typename _Tp1, typename _Tp2>
00185         static constexpr const _Tp1&&
00186         __const_move_get(const std::pair<_Tp1, _Tp2>&& __pair) noexcept
00187         { return std::forward<const _Tp1>(__pair.first); }
00188     };
00189 
00190   template<>
00191     struct __pair_get<1>
00192     {
00193       template<typename _Tp1, typename _Tp2>
00194         static constexpr _Tp2&
00195         __get(std::pair<_Tp1, _Tp2>& __pair) noexcept
00196         { return __pair.second; }
00197 
00198       template<typename _Tp1, typename _Tp2>
00199         static constexpr _Tp2&&
00200         __move_get(std::pair<_Tp1, _Tp2>&& __pair) noexcept
00201         { return std::forward<_Tp2>(__pair.second); }
00202 
00203       template<typename _Tp1, typename _Tp2>
00204         static constexpr const _Tp2&
00205         __const_get(const std::pair<_Tp1, _Tp2>& __pair) noexcept
00206         { return __pair.second; }
00207 
00208       template<typename _Tp1, typename _Tp2>
00209         static constexpr const _Tp2&&
00210         __const_move_get(const std::pair<_Tp1, _Tp2>&& __pair) noexcept
00211         { return std::forward<const _Tp2>(__pair.second); }
00212     };
00213 
00214   template<std::size_t _Int, class _Tp1, class _Tp2>
00215     constexpr typename tuple_element<_Int, std::pair<_Tp1, _Tp2>>::type&
00216     get(std::pair<_Tp1, _Tp2>& __in) noexcept
00217     { return __pair_get<_Int>::__get(__in); }
00218 
00219   template<std::size_t _Int, class _Tp1, class _Tp2>
00220     constexpr typename tuple_element<_Int, std::pair<_Tp1, _Tp2>>::type&&
00221     get(std::pair<_Tp1, _Tp2>&& __in) noexcept
00222     { return __pair_get<_Int>::__move_get(std::move(__in)); }
00223 
00224   template<std::size_t _Int, class _Tp1, class _Tp2>
00225     constexpr const typename tuple_element<_Int, std::pair<_Tp1, _Tp2>>::type&
00226     get(const std::pair<_Tp1, _Tp2>& __in) noexcept
00227     { return __pair_get<_Int>::__const_get(__in); }
00228 
00229   template<std::size_t _Int, class _Tp1, class _Tp2>
00230     constexpr const typename tuple_element<_Int, std::pair<_Tp1, _Tp2>>::type&&
00231     get(const std::pair<_Tp1, _Tp2>&& __in) noexcept
00232     { return __pair_get<_Int>::__const_move_get(std::move(__in)); }
00233 
00234 #if __cplusplus > 201103L
00235 
00236 #define __cpp_lib_tuples_by_type 201304
00237 
00238   template <typename _Tp, typename _Up>
00239     constexpr _Tp&
00240     get(pair<_Tp, _Up>& __p) noexcept
00241     { return __p.first; }
00242 
00243   template <typename _Tp, typename _Up>
00244     constexpr const _Tp&
00245     get(const pair<_Tp, _Up>& __p) noexcept
00246     { return __p.first; }
00247 
00248   template <typename _Tp, typename _Up>
00249     constexpr _Tp&&
00250     get(pair<_Tp, _Up>&& __p) noexcept
00251     { return std::move(__p.first); }
00252 
00253   template <typename _Tp, typename _Up>
00254     constexpr const _Tp&&
00255     get(const pair<_Tp, _Up>&& __p) noexcept
00256     { return std::move(__p.first); }
00257 
00258   template <typename _Tp, typename _Up>
00259     constexpr _Tp&
00260     get(pair<_Up, _Tp>& __p) noexcept
00261     { return __p.second; }
00262 
00263   template <typename _Tp, typename _Up>
00264     constexpr const _Tp&
00265     get(const pair<_Up, _Tp>& __p) noexcept
00266     { return __p.second; }
00267 
00268   template <typename _Tp, typename _Up>
00269     constexpr _Tp&&
00270     get(pair<_Up, _Tp>&& __p) noexcept
00271     { return std::move(__p.second); }
00272 
00273   template <typename _Tp, typename _Up>
00274     constexpr const _Tp&&
00275     get(const pair<_Up, _Tp>&& __p) noexcept
00276     { return std::move(__p.second); }
00277 
00278 #define __cpp_lib_exchange_function 201304
00279 
00280   /// Assign @p __new_val to @p __obj and return its previous value.
00281   template <typename _Tp, typename _Up = _Tp>
00282     inline _Tp
00283     exchange(_Tp& __obj, _Up&& __new_val)
00284     { return std::__exchange(__obj, std::forward<_Up>(__new_val)); }
00285 #endif
00286 
00287   // Stores a tuple of indices.  Used by tuple and pair, and by bind() to
00288   // extract the elements in a tuple.
00289   template<size_t... _Indexes> struct _Index_tuple { };
00290 
00291 #ifdef __has_builtin
00292 # if __has_builtin(__make_integer_seq)
00293 #  define _GLIBCXX_USE_MAKE_INTEGER_SEQ 1
00294 # endif
00295 #endif
00296 
00297   // Builds an _Index_tuple<0, 1, 2, ..., _Num-1>.
00298   template<size_t _Num>
00299     struct _Build_index_tuple
00300     {
00301 #if _GLIBCXX_USE_MAKE_INTEGER_SEQ
00302       template<typename, size_t... _Indices>
00303         using _IdxTuple = _Index_tuple<_Indices...>;
00304 
00305       using __type = __make_integer_seq<_IdxTuple, size_t, _Num>;
00306 #else
00307       using __type = _Index_tuple<__integer_pack(_Num)...>;
00308 #endif
00309     };
00310 
00311 #if __cplusplus > 201103L
00312 
00313 #define __cpp_lib_integer_sequence 201304
00314 
00315   /// Class template integer_sequence
00316   template<typename _Tp, _Tp... _Idx>
00317     struct integer_sequence
00318     {
00319       typedef _Tp value_type;
00320       static constexpr size_t size() noexcept { return sizeof...(_Idx); }
00321     };
00322 
00323   /// Alias template make_integer_sequence
00324   template<typename _Tp, _Tp _Num>
00325     using make_integer_sequence
00326 #if _GLIBCXX_USE_MAKE_INTEGER_SEQ
00327       = __make_integer_seq<integer_sequence, _Tp, _Num>;
00328 #else
00329       = integer_sequence<_Tp, __integer_pack(_Num)...>;
00330 #endif
00331 
00332 #undef _GLIBCXX_USE_MAKE_INTEGER_SEQ
00333 
00334   /// Alias template index_sequence
00335   template<size_t... _Idx>
00336     using index_sequence = integer_sequence<size_t, _Idx...>;
00337 
00338   /// Alias template make_index_sequence
00339   template<size_t _Num>
00340     using make_index_sequence = make_integer_sequence<size_t, _Num>;
00341 
00342   /// Alias template index_sequence_for
00343   template<typename... _Types>
00344     using index_sequence_for = make_index_sequence<sizeof...(_Types)>;
00345 #endif
00346 
00347 #if __cplusplus > 201402L
00348 
00349   struct in_place_t {
00350     explicit in_place_t() = default;
00351   };
00352 
00353   inline constexpr in_place_t in_place{};
00354 
00355   template<typename _Tp> struct in_place_type_t
00356   {
00357     explicit in_place_type_t() = default;
00358   };
00359 
00360   template<typename _Tp>
00361     inline constexpr in_place_type_t<_Tp> in_place_type{};
00362 
00363   template<size_t _Idx> struct in_place_index_t
00364   {
00365     explicit in_place_index_t() = default;
00366   };
00367 
00368   template<size_t _Idx>
00369     inline constexpr in_place_index_t<_Idx> in_place_index{};
00370 
00371   template<typename>
00372     struct __is_in_place_type_impl : false_type
00373     { };
00374 
00375   template<typename _Tp>
00376     struct __is_in_place_type_impl<in_place_type_t<_Tp>> : true_type
00377     { };
00378 
00379   template<typename _Tp>
00380     struct __is_in_place_type
00381       : public __is_in_place_type_impl<_Tp>
00382     { };
00383 
00384 #define  __cpp_lib_as_const 201510
00385   template<typename _Tp>
00386     constexpr add_const_t<_Tp>& as_const(_Tp& __t) noexcept { return __t; }
00387 
00388   template<typename _Tp>
00389     void as_const(const _Tp&&) = delete;
00390 
00391 #endif // C++17
00392 
00393 _GLIBCXX_END_NAMESPACE_VERSION
00394 } // namespace
00395 
00396 #endif
00397 
00398 #endif /* _GLIBCXX_UTILITY */