libstdc++
|
00001 // Implementation of std::reference_wrapper -*- C++ -*- 00002 00003 // Copyright (C) 2004-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 /** @file include/bits/refwrap.h 00026 * This is an internal header file, included by other library headers. 00027 * Do not attempt to use it directly. @headername{functional} 00028 */ 00029 00030 #ifndef _GLIBCXX_REFWRAP_H 00031 #define _GLIBCXX_REFWRAP_H 1 00032 00033 #pragma GCC system_header 00034 00035 #if __cplusplus < 201103L 00036 # include <bits/c++0x_warning.h> 00037 #else 00038 00039 #include <bits/move.h> 00040 #include <bits/invoke.h> 00041 #include <bits/stl_function.h> // for unary_function and binary_function 00042 00043 namespace std _GLIBCXX_VISIBILITY(default) 00044 { 00045 _GLIBCXX_BEGIN_NAMESPACE_VERSION 00046 00047 /** 00048 * Derives from @c unary_function or @c binary_function, or perhaps 00049 * nothing, depending on the number of arguments provided. The 00050 * primary template is the basis case, which derives nothing. 00051 */ 00052 template<typename _Res, typename... _ArgTypes> 00053 struct _Maybe_unary_or_binary_function { }; 00054 00055 /// Derives from @c unary_function, as appropriate. 00056 template<typename _Res, typename _T1> 00057 struct _Maybe_unary_or_binary_function<_Res, _T1> 00058 : std::unary_function<_T1, _Res> { }; 00059 00060 /// Derives from @c binary_function, as appropriate. 00061 template<typename _Res, typename _T1, typename _T2> 00062 struct _Maybe_unary_or_binary_function<_Res, _T1, _T2> 00063 : std::binary_function<_T1, _T2, _Res> { }; 00064 00065 template<typename _Signature> 00066 struct _Mem_fn_traits; 00067 00068 template<typename _Res, typename _Class, typename... _ArgTypes> 00069 struct _Mem_fn_traits_base 00070 { 00071 using __result_type = _Res; 00072 using __maybe_type 00073 = _Maybe_unary_or_binary_function<_Res, _Class*, _ArgTypes...>; 00074 using __arity = integral_constant<size_t, sizeof...(_ArgTypes)>; 00075 }; 00076 00077 #define _GLIBCXX_MEM_FN_TRAITS2(_CV, _REF, _LVAL, _RVAL) \ 00078 template<typename _Res, typename _Class, typename... _ArgTypes> \ 00079 struct _Mem_fn_traits<_Res (_Class::*)(_ArgTypes...) _CV _REF> \ 00080 : _Mem_fn_traits_base<_Res, _CV _Class, _ArgTypes...> \ 00081 { \ 00082 using __vararg = false_type; \ 00083 }; \ 00084 template<typename _Res, typename _Class, typename... _ArgTypes> \ 00085 struct _Mem_fn_traits<_Res (_Class::*)(_ArgTypes... ...) _CV _REF> \ 00086 : _Mem_fn_traits_base<_Res, _CV _Class, _ArgTypes...> \ 00087 { \ 00088 using __vararg = true_type; \ 00089 }; 00090 00091 #define _GLIBCXX_MEM_FN_TRAITS(_REF, _LVAL, _RVAL) \ 00092 _GLIBCXX_MEM_FN_TRAITS2( , _REF, _LVAL, _RVAL) \ 00093 _GLIBCXX_MEM_FN_TRAITS2(const , _REF, _LVAL, _RVAL) \ 00094 _GLIBCXX_MEM_FN_TRAITS2(volatile , _REF, _LVAL, _RVAL) \ 00095 _GLIBCXX_MEM_FN_TRAITS2(const volatile, _REF, _LVAL, _RVAL) 00096 00097 _GLIBCXX_MEM_FN_TRAITS( , true_type, true_type) 00098 _GLIBCXX_MEM_FN_TRAITS(&, true_type, false_type) 00099 _GLIBCXX_MEM_FN_TRAITS(&&, false_type, true_type) 00100 00101 #if __cplusplus > 201402L 00102 _GLIBCXX_MEM_FN_TRAITS(noexcept, true_type, true_type) 00103 _GLIBCXX_MEM_FN_TRAITS(& noexcept, true_type, false_type) 00104 _GLIBCXX_MEM_FN_TRAITS(&& noexcept, false_type, true_type) 00105 #endif 00106 00107 #undef _GLIBCXX_MEM_FN_TRAITS 00108 #undef _GLIBCXX_MEM_FN_TRAITS2 00109 00110 /// If we have found a result_type, extract it. 00111 template<typename _Functor, typename = __void_t<>> 00112 struct _Maybe_get_result_type 00113 { }; 00114 00115 template<typename _Functor> 00116 struct _Maybe_get_result_type<_Functor, 00117 __void_t<typename _Functor::result_type>> 00118 { typedef typename _Functor::result_type result_type; }; 00119 00120 /** 00121 * Base class for any function object that has a weak result type, as 00122 * defined in 20.8.2 [func.require] of C++11. 00123 */ 00124 template<typename _Functor> 00125 struct _Weak_result_type_impl 00126 : _Maybe_get_result_type<_Functor> 00127 { }; 00128 00129 /// Retrieve the result type for a function type. 00130 template<typename _Res, typename... _ArgTypes _GLIBCXX_NOEXCEPT_PARM> 00131 struct _Weak_result_type_impl<_Res(_ArgTypes...) _GLIBCXX_NOEXCEPT_QUAL> 00132 { typedef _Res result_type; }; 00133 00134 /// Retrieve the result type for a varargs function type. 00135 template<typename _Res, typename... _ArgTypes _GLIBCXX_NOEXCEPT_PARM> 00136 struct _Weak_result_type_impl<_Res(_ArgTypes......) _GLIBCXX_NOEXCEPT_QUAL> 00137 { typedef _Res result_type; }; 00138 00139 /// Retrieve the result type for a function pointer. 00140 template<typename _Res, typename... _ArgTypes _GLIBCXX_NOEXCEPT_PARM> 00141 struct _Weak_result_type_impl<_Res(*)(_ArgTypes...) _GLIBCXX_NOEXCEPT_QUAL> 00142 { typedef _Res result_type; }; 00143 00144 /// Retrieve the result type for a varargs function pointer. 00145 template<typename _Res, typename... _ArgTypes _GLIBCXX_NOEXCEPT_PARM> 00146 struct 00147 _Weak_result_type_impl<_Res(*)(_ArgTypes......) _GLIBCXX_NOEXCEPT_QUAL> 00148 { typedef _Res result_type; }; 00149 00150 // Let _Weak_result_type_impl perform the real work. 00151 template<typename _Functor, 00152 bool = is_member_function_pointer<_Functor>::value> 00153 struct _Weak_result_type_memfun 00154 : _Weak_result_type_impl<_Functor> 00155 { }; 00156 00157 // A pointer to member function has a weak result type. 00158 template<typename _MemFunPtr> 00159 struct _Weak_result_type_memfun<_MemFunPtr, true> 00160 { 00161 using result_type = typename _Mem_fn_traits<_MemFunPtr>::__result_type; 00162 }; 00163 00164 // A pointer to data member doesn't have a weak result type. 00165 template<typename _Func, typename _Class> 00166 struct _Weak_result_type_memfun<_Func _Class::*, false> 00167 { }; 00168 00169 /** 00170 * Strip top-level cv-qualifiers from the function object and let 00171 * _Weak_result_type_memfun perform the real work. 00172 */ 00173 template<typename _Functor> 00174 struct _Weak_result_type 00175 : _Weak_result_type_memfun<typename remove_cv<_Functor>::type> 00176 { }; 00177 00178 #if __cplusplus <= 201703L 00179 // Detect nested argument_type. 00180 template<typename _Tp, typename = __void_t<>> 00181 struct _Refwrap_base_arg1 00182 { }; 00183 00184 // Nested argument_type. 00185 template<typename _Tp> 00186 struct _Refwrap_base_arg1<_Tp, 00187 __void_t<typename _Tp::argument_type>> 00188 { 00189 typedef typename _Tp::argument_type argument_type; 00190 }; 00191 00192 // Detect nested first_argument_type and second_argument_type. 00193 template<typename _Tp, typename = __void_t<>> 00194 struct _Refwrap_base_arg2 00195 { }; 00196 00197 // Nested first_argument_type and second_argument_type. 00198 template<typename _Tp> 00199 struct _Refwrap_base_arg2<_Tp, 00200 __void_t<typename _Tp::first_argument_type, 00201 typename _Tp::second_argument_type>> 00202 { 00203 typedef typename _Tp::first_argument_type first_argument_type; 00204 typedef typename _Tp::second_argument_type second_argument_type; 00205 }; 00206 00207 /** 00208 * Derives from unary_function or binary_function when it 00209 * can. Specializations handle all of the easy cases. The primary 00210 * template determines what to do with a class type, which may 00211 * derive from both unary_function and binary_function. 00212 */ 00213 template<typename _Tp> 00214 struct _Reference_wrapper_base 00215 : _Weak_result_type<_Tp>, _Refwrap_base_arg1<_Tp>, _Refwrap_base_arg2<_Tp> 00216 { }; 00217 00218 // - a function type (unary) 00219 template<typename _Res, typename _T1 _GLIBCXX_NOEXCEPT_PARM> 00220 struct _Reference_wrapper_base<_Res(_T1) _GLIBCXX_NOEXCEPT_QUAL> 00221 : unary_function<_T1, _Res> 00222 { }; 00223 00224 template<typename _Res, typename _T1> 00225 struct _Reference_wrapper_base<_Res(_T1) const> 00226 : unary_function<_T1, _Res> 00227 { }; 00228 00229 template<typename _Res, typename _T1> 00230 struct _Reference_wrapper_base<_Res(_T1) volatile> 00231 : unary_function<_T1, _Res> 00232 { }; 00233 00234 template<typename _Res, typename _T1> 00235 struct _Reference_wrapper_base<_Res(_T1) const volatile> 00236 : unary_function<_T1, _Res> 00237 { }; 00238 00239 // - a function type (binary) 00240 template<typename _Res, typename _T1, typename _T2 _GLIBCXX_NOEXCEPT_PARM> 00241 struct _Reference_wrapper_base<_Res(_T1, _T2) _GLIBCXX_NOEXCEPT_QUAL> 00242 : binary_function<_T1, _T2, _Res> 00243 { }; 00244 00245 template<typename _Res, typename _T1, typename _T2> 00246 struct _Reference_wrapper_base<_Res(_T1, _T2) const> 00247 : binary_function<_T1, _T2, _Res> 00248 { }; 00249 00250 template<typename _Res, typename _T1, typename _T2> 00251 struct _Reference_wrapper_base<_Res(_T1, _T2) volatile> 00252 : binary_function<_T1, _T2, _Res> 00253 { }; 00254 00255 template<typename _Res, typename _T1, typename _T2> 00256 struct _Reference_wrapper_base<_Res(_T1, _T2) const volatile> 00257 : binary_function<_T1, _T2, _Res> 00258 { }; 00259 00260 // - a function pointer type (unary) 00261 template<typename _Res, typename _T1 _GLIBCXX_NOEXCEPT_PARM> 00262 struct _Reference_wrapper_base<_Res(*)(_T1) _GLIBCXX_NOEXCEPT_QUAL> 00263 : unary_function<_T1, _Res> 00264 { }; 00265 00266 // - a function pointer type (binary) 00267 template<typename _Res, typename _T1, typename _T2 _GLIBCXX_NOEXCEPT_PARM> 00268 struct _Reference_wrapper_base<_Res(*)(_T1, _T2) _GLIBCXX_NOEXCEPT_QUAL> 00269 : binary_function<_T1, _T2, _Res> 00270 { }; 00271 00272 template<typename _Tp, bool = is_member_function_pointer<_Tp>::value> 00273 struct _Reference_wrapper_base_memfun 00274 : _Reference_wrapper_base<_Tp> 00275 { }; 00276 00277 template<typename _MemFunPtr> 00278 struct _Reference_wrapper_base_memfun<_MemFunPtr, true> 00279 : _Mem_fn_traits<_MemFunPtr>::__maybe_type 00280 { 00281 using result_type = typename _Mem_fn_traits<_MemFunPtr>::__result_type; 00282 }; 00283 #endif // ! C++20 00284 00285 /** 00286 * @brief Primary class template for reference_wrapper. 00287 * @ingroup functors 00288 * @{ 00289 */ 00290 template<typename _Tp> 00291 class reference_wrapper 00292 #if __cplusplus <= 201703L 00293 // In C++20 std::reference_wrapper<T> allows T to be incomplete, 00294 // so checking for nested types could result in ODR violations. 00295 : public _Reference_wrapper_base_memfun<typename remove_cv<_Tp>::type> 00296 #endif 00297 { 00298 _Tp* _M_data; 00299 00300 static _Tp* _S_fun(_Tp& __r) noexcept { return std::__addressof(__r); } 00301 static void _S_fun(_Tp&&) = delete; 00302 00303 template<typename _Up, typename _Up2 = __remove_cvref_t<_Up>> 00304 using __not_same 00305 = typename enable_if<!is_same<reference_wrapper, _Up2>::value>::type; 00306 00307 public: 00308 typedef _Tp type; 00309 00310 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00311 // 2993. reference_wrapper<T> conversion from T&& 00312 // 3041. Unnecessary decay in reference_wrapper 00313 template<typename _Up, typename = __not_same<_Up>, typename 00314 = decltype(reference_wrapper::_S_fun(std::declval<_Up>()))> 00315 reference_wrapper(_Up&& __uref) 00316 noexcept(noexcept(reference_wrapper::_S_fun(std::declval<_Up>()))) 00317 : _M_data(reference_wrapper::_S_fun(std::forward<_Up>(__uref))) 00318 { } 00319 00320 reference_wrapper(const reference_wrapper&) = default; 00321 00322 reference_wrapper& 00323 operator=(const reference_wrapper&) = default; 00324 00325 operator _Tp&() const noexcept 00326 { return this->get(); } 00327 00328 _Tp& 00329 get() const noexcept 00330 { return *_M_data; } 00331 00332 template<typename... _Args> 00333 typename result_of<_Tp&(_Args&&...)>::type 00334 operator()(_Args&&... __args) const 00335 { 00336 #if __cplusplus > 201703L 00337 static_assert(sizeof(type), "type must be complete"); 00338 #endif 00339 return std::__invoke(get(), std::forward<_Args>(__args)...); 00340 } 00341 }; 00342 00343 #if __cpp_deduction_guides 00344 template<typename _Tp> 00345 reference_wrapper(_Tp&) -> reference_wrapper<_Tp>; 00346 #endif 00347 00348 /// Denotes a reference should be taken to a variable. 00349 template<typename _Tp> 00350 inline reference_wrapper<_Tp> 00351 ref(_Tp& __t) noexcept 00352 { return reference_wrapper<_Tp>(__t); } 00353 00354 /// Denotes a const reference should be taken to a variable. 00355 template<typename _Tp> 00356 inline reference_wrapper<const _Tp> 00357 cref(const _Tp& __t) noexcept 00358 { return reference_wrapper<const _Tp>(__t); } 00359 00360 template<typename _Tp> 00361 void ref(const _Tp&&) = delete; 00362 00363 template<typename _Tp> 00364 void cref(const _Tp&&) = delete; 00365 00366 /// std::ref overload to prevent wrapping a reference_wrapper 00367 template<typename _Tp> 00368 inline reference_wrapper<_Tp> 00369 ref(reference_wrapper<_Tp> __t) noexcept 00370 { return __t; } 00371 00372 /// std::cref overload to prevent wrapping a reference_wrapper 00373 template<typename _Tp> 00374 inline reference_wrapper<const _Tp> 00375 cref(reference_wrapper<_Tp> __t) noexcept 00376 { return { __t.get() }; } 00377 00378 // @} group functors 00379 00380 _GLIBCXX_END_NAMESPACE_VERSION 00381 } // namespace std 00382 00383 #endif // C++11 00384 00385 #endif // _GLIBCXX_REFWRAP_H