libstdc++
utility
Go to the documentation of this file.
00001 // <utility> -*- C++ -*-
00002 
00003 // Copyright (C) 2001-2019 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 >= 201402L
00135 // The standard says this macro and alias template should be in <tuple>
00136 // but we define them here, to be available when the partial specializations
00137 // of tuple_element<pair<T,U>> and tuple_element<array<T,N>> are defined.
00138 #define __cpp_lib_tuple_element_t 201402L
00139 
00140   template<std::size_t __i, typename _Tp>
00141     using tuple_element_t = typename tuple_element<__i, _Tp>::type;
00142 #endif
00143 
00144   // Various functions which give std::pair a tuple-like interface.
00145 
00146   /// Partial specialization for std::pair
00147   template<typename _T1, typename _T2>
00148     struct __is_tuple_like_impl<std::pair<_T1, _T2>> : true_type
00149     { };
00150 
00151   /// Partial specialization for std::pair
00152   template<class _Tp1, class _Tp2>
00153     struct tuple_size<std::pair<_Tp1, _Tp2>>
00154     : public integral_constant<std::size_t, 2> { };
00155 
00156   /// Partial specialization for std::pair
00157   template<class _Tp1, class _Tp2>
00158     struct tuple_element<0, std::pair<_Tp1, _Tp2>>
00159     { typedef _Tp1 type; };
00160 
00161   /// Partial specialization for std::pair
00162   template<class _Tp1, class _Tp2>
00163     struct tuple_element<1, std::pair<_Tp1, _Tp2>>
00164     { typedef _Tp2 type; };
00165 
00166   template<std::size_t _Int>
00167     struct __pair_get;
00168 
00169   template<>
00170     struct __pair_get<0>
00171     {
00172       template<typename _Tp1, typename _Tp2>
00173         static constexpr _Tp1&
00174         __get(std::pair<_Tp1, _Tp2>& __pair) noexcept
00175         { return __pair.first; }
00176 
00177       template<typename _Tp1, typename _Tp2>
00178         static constexpr _Tp1&&
00179         __move_get(std::pair<_Tp1, _Tp2>&& __pair) noexcept
00180         { return std::forward<_Tp1>(__pair.first); }
00181 
00182       template<typename _Tp1, typename _Tp2>
00183         static constexpr const _Tp1&
00184         __const_get(const std::pair<_Tp1, _Tp2>& __pair) noexcept
00185         { return __pair.first; }
00186 
00187       template<typename _Tp1, typename _Tp2>
00188         static constexpr const _Tp1&&
00189         __const_move_get(const std::pair<_Tp1, _Tp2>&& __pair) noexcept
00190         { return std::forward<const _Tp1>(__pair.first); }
00191     };
00192 
00193   template<>
00194     struct __pair_get<1>
00195     {
00196       template<typename _Tp1, typename _Tp2>
00197         static constexpr _Tp2&
00198         __get(std::pair<_Tp1, _Tp2>& __pair) noexcept
00199         { return __pair.second; }
00200 
00201       template<typename _Tp1, typename _Tp2>
00202         static constexpr _Tp2&&
00203         __move_get(std::pair<_Tp1, _Tp2>&& __pair) noexcept
00204         { return std::forward<_Tp2>(__pair.second); }
00205 
00206       template<typename _Tp1, typename _Tp2>
00207         static constexpr const _Tp2&
00208         __const_get(const std::pair<_Tp1, _Tp2>& __pair) noexcept
00209         { return __pair.second; }
00210 
00211       template<typename _Tp1, typename _Tp2>
00212         static constexpr const _Tp2&&
00213         __const_move_get(const std::pair<_Tp1, _Tp2>&& __pair) noexcept
00214         { return std::forward<const _Tp2>(__pair.second); }
00215     };
00216 
00217   template<std::size_t _Int, class _Tp1, class _Tp2>
00218     constexpr typename tuple_element<_Int, std::pair<_Tp1, _Tp2>>::type&
00219     get(std::pair<_Tp1, _Tp2>& __in) noexcept
00220     { return __pair_get<_Int>::__get(__in); }
00221 
00222   template<std::size_t _Int, class _Tp1, class _Tp2>
00223     constexpr typename tuple_element<_Int, std::pair<_Tp1, _Tp2>>::type&&
00224     get(std::pair<_Tp1, _Tp2>&& __in) noexcept
00225     { return __pair_get<_Int>::__move_get(std::move(__in)); }
00226 
00227   template<std::size_t _Int, class _Tp1, class _Tp2>
00228     constexpr const typename tuple_element<_Int, std::pair<_Tp1, _Tp2>>::type&
00229     get(const std::pair<_Tp1, _Tp2>& __in) noexcept
00230     { return __pair_get<_Int>::__const_get(__in); }
00231 
00232   template<std::size_t _Int, class _Tp1, class _Tp2>
00233     constexpr const typename tuple_element<_Int, std::pair<_Tp1, _Tp2>>::type&&
00234     get(const std::pair<_Tp1, _Tp2>&& __in) noexcept
00235     { return __pair_get<_Int>::__const_move_get(std::move(__in)); }
00236 
00237 #if __cplusplus > 201103L
00238 
00239 #define __cpp_lib_tuples_by_type 201304
00240 
00241   template <typename _Tp, typename _Up>
00242     constexpr _Tp&
00243     get(pair<_Tp, _Up>& __p) noexcept
00244     { return __p.first; }
00245 
00246   template <typename _Tp, typename _Up>
00247     constexpr const _Tp&
00248     get(const pair<_Tp, _Up>& __p) noexcept
00249     { return __p.first; }
00250 
00251   template <typename _Tp, typename _Up>
00252     constexpr _Tp&&
00253     get(pair<_Tp, _Up>&& __p) noexcept
00254     { return std::move(__p.first); }
00255 
00256   template <typename _Tp, typename _Up>
00257     constexpr const _Tp&&
00258     get(const pair<_Tp, _Up>&& __p) noexcept
00259     { return std::move(__p.first); }
00260 
00261   template <typename _Tp, typename _Up>
00262     constexpr _Tp&
00263     get(pair<_Up, _Tp>& __p) noexcept
00264     { return __p.second; }
00265 
00266   template <typename _Tp, typename _Up>
00267     constexpr const _Tp&
00268     get(const pair<_Up, _Tp>& __p) noexcept
00269     { return __p.second; }
00270 
00271   template <typename _Tp, typename _Up>
00272     constexpr _Tp&&
00273     get(pair<_Up, _Tp>&& __p) noexcept
00274     { return std::move(__p.second); }
00275 
00276   template <typename _Tp, typename _Up>
00277     constexpr const _Tp&&
00278     get(const pair<_Up, _Tp>&& __p) noexcept
00279     { return std::move(__p.second); }
00280 
00281 #define __cpp_lib_exchange_function 201304
00282 
00283   /// Assign @p __new_val to @p __obj and return its previous value.
00284   template <typename _Tp, typename _Up = _Tp>
00285     inline _Tp
00286     exchange(_Tp& __obj, _Up&& __new_val)
00287     { return std::__exchange(__obj, std::forward<_Up>(__new_val)); }
00288 #endif
00289 
00290   // Stores a tuple of indices.  Used by tuple and pair, and by bind() to
00291   // extract the elements in a tuple.
00292   template<size_t... _Indexes> struct _Index_tuple { };
00293 
00294 #ifdef __has_builtin
00295 # if __has_builtin(__make_integer_seq)
00296 #  define _GLIBCXX_USE_MAKE_INTEGER_SEQ 1
00297 # endif
00298 #endif
00299 
00300   // Builds an _Index_tuple<0, 1, 2, ..., _Num-1>.
00301   template<size_t _Num>
00302     struct _Build_index_tuple
00303     {
00304 #if _GLIBCXX_USE_MAKE_INTEGER_SEQ
00305       template<typename, size_t... _Indices>
00306         using _IdxTuple = _Index_tuple<_Indices...>;
00307 
00308       using __type = __make_integer_seq<_IdxTuple, size_t, _Num>;
00309 #else
00310       using __type = _Index_tuple<__integer_pack(_Num)...>;
00311 #endif
00312     };
00313 
00314 #if __cplusplus > 201103L
00315 
00316 #define __cpp_lib_integer_sequence 201304
00317 
00318   /// Class template integer_sequence
00319   template<typename _Tp, _Tp... _Idx>
00320     struct integer_sequence
00321     {
00322       typedef _Tp value_type;
00323       static constexpr size_t size() noexcept { return sizeof...(_Idx); }
00324     };
00325 
00326   /// Alias template make_integer_sequence
00327   template<typename _Tp, _Tp _Num>
00328     using make_integer_sequence
00329 #if _GLIBCXX_USE_MAKE_INTEGER_SEQ
00330       = __make_integer_seq<integer_sequence, _Tp, _Num>;
00331 #else
00332       = integer_sequence<_Tp, __integer_pack(_Num)...>;
00333 #endif
00334 
00335 #undef _GLIBCXX_USE_MAKE_INTEGER_SEQ
00336 
00337   /// Alias template index_sequence
00338   template<size_t... _Idx>
00339     using index_sequence = integer_sequence<size_t, _Idx...>;
00340 
00341   /// Alias template make_index_sequence
00342   template<size_t _Num>
00343     using make_index_sequence = make_integer_sequence<size_t, _Num>;
00344 
00345   /// Alias template index_sequence_for
00346   template<typename... _Types>
00347     using index_sequence_for = make_index_sequence<sizeof...(_Types)>;
00348 #endif
00349 
00350 #if __cplusplus > 201402L
00351 
00352   struct in_place_t {
00353     explicit in_place_t() = default;
00354   };
00355 
00356   inline constexpr in_place_t in_place{};
00357 
00358   template<typename _Tp> struct in_place_type_t
00359   {
00360     explicit in_place_type_t() = default;
00361   };
00362 
00363   template<typename _Tp>
00364     inline constexpr in_place_type_t<_Tp> in_place_type{};
00365 
00366   template<size_t _Idx> struct in_place_index_t
00367   {
00368     explicit in_place_index_t() = default;
00369   };
00370 
00371   template<size_t _Idx>
00372     inline constexpr in_place_index_t<_Idx> in_place_index{};
00373 
00374   template<typename>
00375     struct __is_in_place_type_impl : false_type
00376     { };
00377 
00378   template<typename _Tp>
00379     struct __is_in_place_type_impl<in_place_type_t<_Tp>> : true_type
00380     { };
00381 
00382   template<typename _Tp>
00383     struct __is_in_place_type
00384       : public __is_in_place_type_impl<_Tp>
00385     { };
00386 
00387 #define  __cpp_lib_as_const 201510
00388   template<typename _Tp>
00389     constexpr add_const_t<_Tp>& as_const(_Tp& __t) noexcept { return __t; }
00390 
00391   template<typename _Tp>
00392     void as_const(const _Tp&&) = delete;
00393 
00394 #endif // C++17
00395 
00396 _GLIBCXX_END_NAMESPACE_VERSION
00397 } // namespace
00398 
00399 #endif
00400 
00401 #endif /* _GLIBCXX_UTILITY */