1// Implementation of std::reference_wrapper -*- C++ -*-
2
3// Copyright (C) 2004-2019 Free Software Foundation, Inc.
4//
5// This file is part of the GNU ISO C++ Library. This library is free
6// software; you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 3, or (at your option)
9// any later version.
10
11// This library is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// Under Section 7 of GPL version 3, you are granted additional
17// permissions described in the GCC Runtime Library Exception, version
18// 3.1, as published by the Free Software Foundation.
19
20// You should have received a copy of the GNU General Public License and
21// a copy of the GCC Runtime Library Exception along with this program;
22// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23// <http://www.gnu.org/licenses/>.
24
25/** @file include/bits/refwrap.h
26 * This is an internal header file, included by other library headers.
27 * Do not attempt to use it directly. @headername{functional}
28 */
29
30#ifndef _GLIBCXX_REFWRAP_H
31#define _GLIBCXX_REFWRAP_H 1
32
33#pragma GCC system_header
34
35#if __cplusplus < 201103L
36# include <bits/c++0x_warning.h>
37#else
38
39#include <bits/move.h>
40#include <bits/invoke.h>
41#include <bits/stl_function.h> // for unary_function and binary_function
42
43namespace std _GLIBCXX_VISIBILITY(default)
44{
45_GLIBCXX_BEGIN_NAMESPACE_VERSION
46
47 /**
48 * Derives from @c unary_function or @c binary_function, or perhaps
49 * nothing, depending on the number of arguments provided. The
50 * primary template is the basis case, which derives nothing.
51 */
52 template<typename _Res, typename... _ArgTypes>
53 struct _Maybe_unary_or_binary_function { };
54
55 /// Derives from @c unary_function, as appropriate.
56 template<typename _Res, typename _T1>
57 struct _Maybe_unary_or_binary_function<_Res, _T1>
58 : std::unary_function<_T1, _Res> { };
59
60 /// Derives from @c binary_function, as appropriate.
61 template<typename _Res, typename _T1, typename _T2>
62 struct _Maybe_unary_or_binary_function<_Res, _T1, _T2>
63 : std::binary_function<_T1, _T2, _Res> { };
64
65 template<typename _Signature>
66 struct _Mem_fn_traits;
67
68 template<typename _Res, typename _Class, typename... _ArgTypes>
69 struct _Mem_fn_traits_base
70 {
71 using __result_type = _Res;
72 using __maybe_type
73 = _Maybe_unary_or_binary_function<_Res, _Class*, _ArgTypes...>;
74 using __arity = integral_constant<size_t, sizeof...(_ArgTypes)>;
75 };
76
77#define _GLIBCXX_MEM_FN_TRAITS2(_CV, _REF, _LVAL, _RVAL) \
78 template<typename _Res, typename _Class, typename... _ArgTypes> \
79 struct _Mem_fn_traits<_Res (_Class::*)(_ArgTypes...) _CV _REF> \
80 : _Mem_fn_traits_base<_Res, _CV _Class, _ArgTypes...> \
81 { \
82 using __vararg = false_type; \
83 }; \
84 template<typename _Res, typename _Class, typename... _ArgTypes> \
85 struct _Mem_fn_traits<_Res (_Class::*)(_ArgTypes... ...) _CV _REF> \
86 : _Mem_fn_traits_base<_Res, _CV _Class, _ArgTypes...> \
87 { \
88 using __vararg = true_type; \
89 };
90
91#define _GLIBCXX_MEM_FN_TRAITS(_REF, _LVAL, _RVAL) \
92 _GLIBCXX_MEM_FN_TRAITS2( , _REF, _LVAL, _RVAL) \
93 _GLIBCXX_MEM_FN_TRAITS2(const , _REF, _LVAL, _RVAL) \
94 _GLIBCXX_MEM_FN_TRAITS2(volatile , _REF, _LVAL, _RVAL) \
95 _GLIBCXX_MEM_FN_TRAITS2(const volatile, _REF, _LVAL, _RVAL)
96
97_GLIBCXX_MEM_FN_TRAITS( , true_type, true_type)
98_GLIBCXX_MEM_FN_TRAITS(&, true_type, false_type)
99_GLIBCXX_MEM_FN_TRAITS(&&, false_type, true_type)
100
101#if __cplusplus > 201402L
102_GLIBCXX_MEM_FN_TRAITS(noexcept, true_type, true_type)
103_GLIBCXX_MEM_FN_TRAITS(& noexcept, true_type, false_type)
104_GLIBCXX_MEM_FN_TRAITS(&& noexcept, false_type, true_type)
105#endif
106
107#undef _GLIBCXX_MEM_FN_TRAITS
108#undef _GLIBCXX_MEM_FN_TRAITS2
109
110 /// If we have found a result_type, extract it.
111 template<typename _Functor, typename = __void_t<>>
112 struct _Maybe_get_result_type
113 { };
114
115 template<typename _Functor>
116 struct _Maybe_get_result_type<_Functor,
117 __void_t<typename _Functor::result_type>>
118 { typedef typename _Functor::result_type result_type; };
119
120 /**
121 * Base class for any function object that has a weak result type, as
122 * defined in 20.8.2 [func.require] of C++11.
123 */
124 template<typename _Functor>
125 struct _Weak_result_type_impl
126 : _Maybe_get_result_type<_Functor>
127 { };
128
129 /// Retrieve the result type for a function type.
130 template<typename _Res, typename... _ArgTypes _GLIBCXX_NOEXCEPT_PARM>
131 struct _Weak_result_type_impl<_Res(_ArgTypes...) _GLIBCXX_NOEXCEPT_QUAL>
132 { typedef _Res result_type; };
133
134 /// Retrieve the result type for a varargs function type.
135 template<typename _Res, typename... _ArgTypes _GLIBCXX_NOEXCEPT_PARM>
136 struct _Weak_result_type_impl<_Res(_ArgTypes......) _GLIBCXX_NOEXCEPT_QUAL>
137 { typedef _Res result_type; };
138
139 /// Retrieve the result type for a function pointer.
140 template<typename _Res, typename... _ArgTypes _GLIBCXX_NOEXCEPT_PARM>
141 struct _Weak_result_type_impl<_Res(*)(_ArgTypes...) _GLIBCXX_NOEXCEPT_QUAL>
142 { typedef _Res result_type; };
143
144 /// Retrieve the result type for a varargs function pointer.
145 template<typename _Res, typename... _ArgTypes _GLIBCXX_NOEXCEPT_PARM>
146 struct
147 _Weak_result_type_impl<_Res(*)(_ArgTypes......) _GLIBCXX_NOEXCEPT_QUAL>
148 { typedef _Res result_type; };
149
150 // Let _Weak_result_type_impl perform the real work.
151 template<typename _Functor,
152 bool = is_member_function_pointer<_Functor>::value>
153 struct _Weak_result_type_memfun
154 : _Weak_result_type_impl<_Functor>
155 { };
156
157 // A pointer to member function has a weak result type.
158 template<typename _MemFunPtr>
159 struct _Weak_result_type_memfun<_MemFunPtr, true>
160 {
161 using result_type = typename _Mem_fn_traits<_MemFunPtr>::__result_type;
162 };
163
164 // A pointer to data member doesn't have a weak result type.
165 template<typename _Func, typename _Class>
166 struct _Weak_result_type_memfun<_Func _Class::*, false>
167 { };
168
169 /**
170 * Strip top-level cv-qualifiers from the function object and let
171 * _Weak_result_type_memfun perform the real work.
172 */
173 template<typename _Functor>
174 struct _Weak_result_type
175 : _Weak_result_type_memfun<typename remove_cv<_Functor>::type>
176 { };
177
178#if __cplusplus <= 201703L
179 // Detect nested argument_type.
180 template<typename _Tp, typename = __void_t<>>
181 struct _Refwrap_base_arg1
182 { };
183
184 // Nested argument_type.
185 template<typename _Tp>
186 struct _Refwrap_base_arg1<_Tp,
187 __void_t<typename _Tp::argument_type>>
188 {
189 typedef typename _Tp::argument_type argument_type;
190 };
191
192 // Detect nested first_argument_type and second_argument_type.
193 template<typename _Tp, typename = __void_t<>>
194 struct _Refwrap_base_arg2
195 { };
196
197 // Nested first_argument_type and second_argument_type.
198 template<typename _Tp>
199 struct _Refwrap_base_arg2<_Tp,
200 __void_t<typename _Tp::first_argument_type,
201 typename _Tp::second_argument_type>>
202 {
203 typedef typename _Tp::first_argument_type first_argument_type;
204 typedef typename _Tp::second_argument_type second_argument_type;
205 };
206
207 /**
208 * Derives from unary_function or binary_function when it
209 * can. Specializations handle all of the easy cases. The primary
210 * template determines what to do with a class type, which may
211 * derive from both unary_function and binary_function.
212 */
213 template<typename _Tp>
214 struct _Reference_wrapper_base
215 : _Weak_result_type<_Tp>, _Refwrap_base_arg1<_Tp>, _Refwrap_base_arg2<_Tp>
216 { };
217
218 // - a function type (unary)
219 template<typename _Res, typename _T1 _GLIBCXX_NOEXCEPT_PARM>
220 struct _Reference_wrapper_base<_Res(_T1) _GLIBCXX_NOEXCEPT_QUAL>
221 : unary_function<_T1, _Res>
222 { };
223
224 template<typename _Res, typename _T1>
225 struct _Reference_wrapper_base<_Res(_T1) const>
226 : unary_function<_T1, _Res>
227 { };
228
229 template<typename _Res, typename _T1>
230 struct _Reference_wrapper_base<_Res(_T1) volatile>
231 : unary_function<_T1, _Res>
232 { };
233
234 template<typename _Res, typename _T1>
235 struct _Reference_wrapper_base<_Res(_T1) const volatile>
236 : unary_function<_T1, _Res>
237 { };
238
239 // - a function type (binary)
240 template<typename _Res, typename _T1, typename _T2 _GLIBCXX_NOEXCEPT_PARM>
241 struct _Reference_wrapper_base<_Res(_T1, _T2) _GLIBCXX_NOEXCEPT_QUAL>
242 : binary_function<_T1, _T2, _Res>
243 { };
244
245 template<typename _Res, typename _T1, typename _T2>
246 struct _Reference_wrapper_base<_Res(_T1, _T2) const>
247 : binary_function<_T1, _T2, _Res>
248 { };
249
250 template<typename _Res, typename _T1, typename _T2>
251 struct _Reference_wrapper_base<_Res(_T1, _T2) volatile>
252 : binary_function<_T1, _T2, _Res>
253 { };
254
255 template<typename _Res, typename _T1, typename _T2>
256 struct _Reference_wrapper_base<_Res(_T1, _T2) const volatile>
257 : binary_function<_T1, _T2, _Res>
258 { };
259
260 // - a function pointer type (unary)
261 template<typename _Res, typename _T1 _GLIBCXX_NOEXCEPT_PARM>
262 struct _Reference_wrapper_base<_Res(*)(_T1) _GLIBCXX_NOEXCEPT_QUAL>
263 : unary_function<_T1, _Res>
264 { };
265
266 // - a function pointer type (binary)
267 template<typename _Res, typename _T1, typename _T2 _GLIBCXX_NOEXCEPT_PARM>
268 struct _Reference_wrapper_base<_Res(*)(_T1, _T2) _GLIBCXX_NOEXCEPT_QUAL>
269 : binary_function<_T1, _T2, _Res>
270 { };
271
272 template<typename _Tp, bool = is_member_function_pointer<_Tp>::value>
273 struct _Reference_wrapper_base_memfun
274 : _Reference_wrapper_base<_Tp>
275 { };
276
277 template<typename _MemFunPtr>
278 struct _Reference_wrapper_base_memfun<_MemFunPtr, true>
279 : _Mem_fn_traits<_MemFunPtr>::__maybe_type
280 {
281 using result_type = typename _Mem_fn_traits<_MemFunPtr>::__result_type;
282 };
283#endif // ! C++20
284
285 /**
286 * @brief Primary class template for reference_wrapper.
287 * @ingroup functors
288 * @{
289 */
290 template<typename _Tp>
291 class reference_wrapper
292#if __cplusplus <= 201703L
293 // In C++20 std::reference_wrapper<T> allows T to be incomplete,
294 // so checking for nested types could result in ODR violations.
295 : public _Reference_wrapper_base_memfun<typename remove_cv<_Tp>::type>
296#endif
297 {
298 _Tp* _M_data;
299
300 static _Tp* _S_fun(_Tp& __r) noexcept { return std::__addressof(__r); }
301 static void _S_fun(_Tp&&) = delete;
302
303 template<typename _Up, typename _Up2 = __remove_cvref_t<_Up>>
304 using __not_same
305 = typename enable_if<!is_same<reference_wrapper, _Up2>::value>::type;
306
307 public:
308 typedef _Tp type;
309
310 // _GLIBCXX_RESOLVE_LIB_DEFECTS
311 // 2993. reference_wrapper<T> conversion from T&&
312 // 3041. Unnecessary decay in reference_wrapper
313 template<typename _Up, typename = __not_same<_Up>, typename
314 = decltype(reference_wrapper::_S_fun(std::declval<_Up>()))>
315 reference_wrapper(_Up&& __uref)
316 noexcept(noexcept(reference_wrapper::_S_fun(std::declval<_Up>())))
317 : _M_data(reference_wrapper::_S_fun(std::forward<_Up>(__uref)))
318 { }
319
320 reference_wrapper(const reference_wrapper&) = default;
321
322 reference_wrapper&
323 operator=(const reference_wrapper&) = default;
324
325 operator _Tp&() const noexcept
326 { return this->get(); }
327
328 _Tp&
329 get() const noexcept
330 { return *_M_data; }
331
332 template<typename... _Args>
333 typename result_of<_Tp&(_Args&&...)>::type
334 operator()(_Args&&... __args) const
335 {
336#if __cplusplus > 201703L
337 if constexpr (is_object_v<type>)
338 static_assert(sizeof(type), "type must be complete");
339#endif
340 return std::__invoke(get(), std::forward<_Args>(__args)...);
341 }
342 };
343
344#if __cpp_deduction_guides
345 template<typename _Tp>
346 reference_wrapper(_Tp&) -> reference_wrapper<_Tp>;
347#endif
348
349 /// Denotes a reference should be taken to a variable.
350 template<typename _Tp>
351 inline reference_wrapper<_Tp>
352 ref(_Tp& __t) noexcept
353 { return reference_wrapper<_Tp>(__t); }
354
355 /// Denotes a const reference should be taken to a variable.
356 template<typename _Tp>
357 inline reference_wrapper<const _Tp>
358 cref(const _Tp& __t) noexcept
359 { return reference_wrapper<const _Tp>(__t); }
360
361 template<typename _Tp>
362 void ref(const _Tp&&) = delete;
363
364 template<typename _Tp>
365 void cref(const _Tp&&) = delete;
366
367 /// std::ref overload to prevent wrapping a reference_wrapper
368 template<typename _Tp>
369 inline reference_wrapper<_Tp>
370 ref(reference_wrapper<_Tp> __t) noexcept
371 { return __t; }
372
373 /// std::cref overload to prevent wrapping a reference_wrapper
374 template<typename _Tp>
375 inline reference_wrapper<const _Tp>
376 cref(reference_wrapper<_Tp> __t) noexcept
377 { return { __t.get() }; }
378
379 /// @} group functors
380
381_GLIBCXX_END_NAMESPACE_VERSION
382} // namespace std
383
384#endif // C++11
385
386#endif // _GLIBCXX_REFWRAP_H
387