libstdc++
|
00001 // <utility> -*- C++ -*- 00002 00003 // Copyright (C) 2001-2017 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 #if __cplusplus > 201402L 00079 #include <exception> 00080 #endif 00081 00082 namespace std _GLIBCXX_VISIBILITY(default) 00083 { 00084 _GLIBCXX_BEGIN_NAMESPACE_VERSION 00085 00086 /// Finds the size of a given tuple type. 00087 template<typename _Tp> 00088 struct tuple_size; 00089 00090 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00091 // 2770. tuple_size<const T> specialization is not SFINAE compatible 00092 00093 #if __cplusplus <= 201402L 00094 template<typename _Tp, typename = void> 00095 struct __tuple_size_cv_impl { }; 00096 00097 template<typename _Tp> 00098 struct __tuple_size_cv_impl<_Tp, __void_t<decltype(tuple_size<_Tp>::value)>> 00099 : integral_constant<size_t, tuple_size<_Tp>::value> { }; 00100 00101 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00102 // 2313. tuple_size should always derive from integral_constant<size_t, N> 00103 template<typename _Tp> 00104 struct tuple_size<const _Tp> : __tuple_size_cv_impl<_Tp> { }; 00105 00106 template<typename _Tp> 00107 struct tuple_size<volatile _Tp> : __tuple_size_cv_impl<_Tp> { }; 00108 00109 template<typename _Tp> 00110 struct tuple_size<const volatile _Tp> : __tuple_size_cv_impl<_Tp> { }; 00111 #else 00112 template<typename _Tp, 00113 typename _Up = typename remove_cv<_Tp>::type, 00114 typename = typename enable_if<is_same<_Tp, _Up>::value>::type, 00115 size_t = tuple_size<_Tp>::value> 00116 using __enable_if_has_tuple_size = _Tp; 00117 00118 template<typename _Tp> 00119 struct tuple_size<const __enable_if_has_tuple_size<_Tp>> 00120 : public tuple_size<_Tp> { }; 00121 00122 template<typename _Tp> 00123 struct tuple_size<volatile __enable_if_has_tuple_size<_Tp>> 00124 : public tuple_size<_Tp> { }; 00125 00126 template<typename _Tp> 00127 struct tuple_size<const volatile __enable_if_has_tuple_size<_Tp>> 00128 : public tuple_size<_Tp> { }; 00129 #endif 00130 00131 /// Gives the type of the ith element of a given tuple type. 00132 template<std::size_t __i, typename _Tp> 00133 struct tuple_element; 00134 00135 // Duplicate of C++14's tuple_element_t for internal use in C++11 mode 00136 template<std::size_t __i, typename _Tp> 00137 using __tuple_element_t = typename tuple_element<__i, _Tp>::type; 00138 00139 template<std::size_t __i, typename _Tp> 00140 struct tuple_element<__i, const _Tp> 00141 { 00142 typedef typename add_const<__tuple_element_t<__i, _Tp>>::type type; 00143 }; 00144 00145 template<std::size_t __i, typename _Tp> 00146 struct tuple_element<__i, volatile _Tp> 00147 { 00148 typedef typename add_volatile<__tuple_element_t<__i, _Tp>>::type type; 00149 }; 00150 00151 template<std::size_t __i, typename _Tp> 00152 struct tuple_element<__i, const volatile _Tp> 00153 { 00154 typedef typename add_cv<__tuple_element_t<__i, _Tp>>::type type; 00155 }; 00156 00157 #if __cplusplus > 201103L 00158 #define __cpp_lib_tuple_element_t 201402 00159 00160 template<std::size_t __i, typename _Tp> 00161 using tuple_element_t = typename tuple_element<__i, _Tp>::type; 00162 #endif 00163 00164 // Various functions which give std::pair a tuple-like interface. 00165 00166 /// Partial specialization for std::pair 00167 template<typename _T1, typename _T2> 00168 struct __is_tuple_like_impl<std::pair<_T1, _T2>> : true_type 00169 { }; 00170 00171 /// Partial specialization for std::pair 00172 template<class _Tp1, class _Tp2> 00173 struct tuple_size<std::pair<_Tp1, _Tp2>> 00174 : public integral_constant<std::size_t, 2> { }; 00175 00176 /// Partial specialization for std::pair 00177 template<class _Tp1, class _Tp2> 00178 struct tuple_element<0, std::pair<_Tp1, _Tp2>> 00179 { typedef _Tp1 type; }; 00180 00181 /// Partial specialization for std::pair 00182 template<class _Tp1, class _Tp2> 00183 struct tuple_element<1, std::pair<_Tp1, _Tp2>> 00184 { typedef _Tp2 type; }; 00185 00186 template<std::size_t _Int> 00187 struct __pair_get; 00188 00189 template<> 00190 struct __pair_get<0> 00191 { 00192 template<typename _Tp1, typename _Tp2> 00193 static constexpr _Tp1& 00194 __get(std::pair<_Tp1, _Tp2>& __pair) noexcept 00195 { return __pair.first; } 00196 00197 template<typename _Tp1, typename _Tp2> 00198 static constexpr _Tp1&& 00199 __move_get(std::pair<_Tp1, _Tp2>&& __pair) noexcept 00200 { return std::forward<_Tp1>(__pair.first); } 00201 00202 template<typename _Tp1, typename _Tp2> 00203 static constexpr const _Tp1& 00204 __const_get(const std::pair<_Tp1, _Tp2>& __pair) noexcept 00205 { return __pair.first; } 00206 }; 00207 00208 template<> 00209 struct __pair_get<1> 00210 { 00211 template<typename _Tp1, typename _Tp2> 00212 static constexpr _Tp2& 00213 __get(std::pair<_Tp1, _Tp2>& __pair) noexcept 00214 { return __pair.second; } 00215 00216 template<typename _Tp1, typename _Tp2> 00217 static constexpr _Tp2&& 00218 __move_get(std::pair<_Tp1, _Tp2>&& __pair) noexcept 00219 { return std::forward<_Tp2>(__pair.second); } 00220 00221 template<typename _Tp1, typename _Tp2> 00222 static constexpr const _Tp2& 00223 __const_get(const std::pair<_Tp1, _Tp2>& __pair) noexcept 00224 { return __pair.second; } 00225 }; 00226 00227 template<std::size_t _Int, class _Tp1, class _Tp2> 00228 constexpr typename tuple_element<_Int, std::pair<_Tp1, _Tp2>>::type& 00229 get(std::pair<_Tp1, _Tp2>& __in) noexcept 00230 { return __pair_get<_Int>::__get(__in); } 00231 00232 template<std::size_t _Int, class _Tp1, class _Tp2> 00233 constexpr typename tuple_element<_Int, std::pair<_Tp1, _Tp2>>::type&& 00234 get(std::pair<_Tp1, _Tp2>&& __in) noexcept 00235 { return __pair_get<_Int>::__move_get(std::move(__in)); } 00236 00237 template<std::size_t _Int, class _Tp1, class _Tp2> 00238 constexpr const typename tuple_element<_Int, std::pair<_Tp1, _Tp2>>::type& 00239 get(const std::pair<_Tp1, _Tp2>& __in) noexcept 00240 { return __pair_get<_Int>::__const_get(__in); } 00241 00242 #if __cplusplus > 201103L 00243 00244 #define __cpp_lib_tuples_by_type 201304 00245 00246 template <typename _Tp, typename _Up> 00247 constexpr _Tp& 00248 get(pair<_Tp, _Up>& __p) noexcept 00249 { return __p.first; } 00250 00251 template <typename _Tp, typename _Up> 00252 constexpr const _Tp& 00253 get(const pair<_Tp, _Up>& __p) noexcept 00254 { return __p.first; } 00255 00256 template <typename _Tp, typename _Up> 00257 constexpr _Tp&& 00258 get(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 #define __cpp_lib_exchange_function 201304 00277 00278 /// Assign @p __new_val to @p __obj and return its previous value. 00279 template <typename _Tp, typename _Up = _Tp> 00280 inline _Tp 00281 exchange(_Tp& __obj, _Up&& __new_val) 00282 { return std::__exchange(__obj, std::forward<_Up>(__new_val)); } 00283 #endif 00284 00285 // Stores a tuple of indices. Used by tuple and pair, and by bind() to 00286 // extract the elements in a tuple. 00287 template<size_t... _Indexes> struct _Index_tuple { }; 00288 00289 // Concatenates two _Index_tuples. 00290 template<typename _Itup1, typename _Itup2> struct _Itup_cat; 00291 00292 template<size_t... _Ind1, size_t... _Ind2> 00293 struct _Itup_cat<_Index_tuple<_Ind1...>, _Index_tuple<_Ind2...>> 00294 { 00295 using __type = _Index_tuple<_Ind1..., (_Ind2 + sizeof...(_Ind1))...>; 00296 }; 00297 00298 // Builds an _Index_tuple<0, 1, 2, ..., _Num-1>. 00299 template<size_t _Num> 00300 struct _Build_index_tuple 00301 : _Itup_cat<typename _Build_index_tuple<_Num / 2>::__type, 00302 typename _Build_index_tuple<_Num - _Num / 2>::__type> 00303 { }; 00304 00305 template<> 00306 struct _Build_index_tuple<1> 00307 { 00308 typedef _Index_tuple<0> __type; 00309 }; 00310 00311 template<> 00312 struct _Build_index_tuple<0> 00313 { 00314 typedef _Index_tuple<> __type; 00315 }; 00316 00317 #if __cplusplus > 201103L 00318 00319 #define __cpp_lib_integer_sequence 201304 00320 00321 /// Class template integer_sequence 00322 template<typename _Tp, _Tp... _Idx> 00323 struct integer_sequence 00324 { 00325 typedef _Tp value_type; 00326 static constexpr size_t size() { return sizeof...(_Idx); } 00327 }; 00328 00329 template<typename _Tp, _Tp _Num, 00330 typename _ISeq = typename _Build_index_tuple<_Num>::__type> 00331 struct _Make_integer_sequence; 00332 00333 template<typename _Tp, _Tp _Num, size_t... _Idx> 00334 struct _Make_integer_sequence<_Tp, _Num, _Index_tuple<_Idx...>> 00335 { 00336 static_assert( _Num >= 0, 00337 "Cannot make integer sequence of negative length" ); 00338 00339 typedef integer_sequence<_Tp, static_cast<_Tp>(_Idx)...> __type; 00340 }; 00341 00342 /// Alias template make_integer_sequence 00343 template<typename _Tp, _Tp _Num> 00344 using make_integer_sequence 00345 = typename _Make_integer_sequence<_Tp, _Num>::__type; 00346 00347 /// Alias template index_sequence 00348 template<size_t... _Idx> 00349 using index_sequence = integer_sequence<size_t, _Idx...>; 00350 00351 /// Alias template make_index_sequence 00352 template<size_t _Num> 00353 using make_index_sequence = make_integer_sequence<size_t, _Num>; 00354 00355 /// Alias template index_sequence_for 00356 template<typename... _Types> 00357 using index_sequence_for = make_index_sequence<sizeof...(_Types)>; 00358 #endif 00359 00360 #if __cplusplus > 201402L 00361 00362 struct in_place_t { 00363 explicit in_place_t() = default; 00364 }; 00365 00366 inline constexpr in_place_t in_place{}; 00367 00368 template<typename _Tp> struct in_place_type_t 00369 { 00370 explicit in_place_type_t() = default; 00371 }; 00372 00373 template<typename _Tp> 00374 inline constexpr in_place_type_t<_Tp> in_place_type{}; 00375 00376 template<size_t _Idx> struct in_place_index_t 00377 { 00378 explicit in_place_index_t() = default; 00379 }; 00380 00381 template<size_t _Idx> 00382 inline constexpr in_place_index_t<_Idx> in_place_index{}; 00383 00384 template<typename> 00385 struct __is_in_place_type_impl : false_type 00386 { }; 00387 00388 template<typename _Tp> 00389 struct __is_in_place_type_impl<in_place_type_t<_Tp>> : true_type 00390 { }; 00391 00392 template<typename _Tp> 00393 struct __is_in_place_type 00394 : public __is_in_place_type_impl<_Tp> 00395 { }; 00396 00397 #define __cpp_lib_as_const 201510 00398 template<typename _Tp> 00399 constexpr add_const_t<_Tp>& as_const(_Tp& __t) noexcept { return __t; } 00400 00401 template<typename _Tp> 00402 void as_const(const _Tp&&) = delete; 00403 00404 #endif // C++17 00405 00406 _GLIBCXX_END_NAMESPACE_VERSION 00407 } // namespace 00408 00409 #endif 00410 00411 #endif /* _GLIBCXX_UTILITY */