libstdc++
refwrap.h
Go to the documentation of this file.
1// Implementation of std::reference_wrapper -*- C++ -*-
2
3// Copyright (C) 2004-2024 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#ifdef _GLIBCXX_SYSHDR
34#pragma GCC system_header
35#endif
36
37#if __cplusplus >= 201103L
38
39#include <bits/move.h>
40#include <bits/invoke.h>
41#include <bits/stl_function.h> // for unary_function and binary_function
42
43#if __glibcxx_reference_wrapper >= 202403L // >= C++26
44# include <compare>
45#endif
46
47namespace std _GLIBCXX_VISIBILITY(default)
48{
49_GLIBCXX_BEGIN_NAMESPACE_VERSION
50
51 /// @cond undocumented
52
53 /**
54 * Derives from @c unary_function or @c binary_function, or perhaps
55 * nothing, depending on the number of arguments provided. The
56 * primary template is the basis case, which derives nothing.
57 */
58 template<typename _Res, typename... _ArgTypes>
59 struct _Maybe_unary_or_binary_function { };
60
61// Ignore warnings about unary_function and binary_function.
62#pragma GCC diagnostic push
63#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
64
65 /// Derives from @c unary_function, as appropriate.
66 template<typename _Res, typename _T1>
67 struct _Maybe_unary_or_binary_function<_Res, _T1>
68 : std::unary_function<_T1, _Res> { };
69
70 /// Derives from @c binary_function, as appropriate.
71 template<typename _Res, typename _T1, typename _T2>
72 struct _Maybe_unary_or_binary_function<_Res, _T1, _T2>
73 : std::binary_function<_T1, _T2, _Res> { };
74
75#pragma GCC diagnostic pop
76
77 template<typename _Signature>
78 struct _Mem_fn_traits;
79
80 template<typename _Res, typename _Class, typename... _ArgTypes>
81 struct _Mem_fn_traits_base
82 {
83 using __result_type = _Res;
84 using __maybe_type
85 = _Maybe_unary_or_binary_function<_Res, _Class*, _ArgTypes...>;
86 using __arity = integral_constant<size_t, sizeof...(_ArgTypes)>;
87 };
88
89#define _GLIBCXX_MEM_FN_TRAITS2(_CV, _REF, _LVAL, _RVAL) \
90 template<typename _Res, typename _Class, typename... _ArgTypes> \
91 struct _Mem_fn_traits<_Res (_Class::*)(_ArgTypes...) _CV _REF> \
92 : _Mem_fn_traits_base<_Res, _CV _Class, _ArgTypes...> \
93 { \
94 using __vararg = false_type; \
95 }; \
96 template<typename _Res, typename _Class, typename... _ArgTypes> \
97 struct _Mem_fn_traits<_Res (_Class::*)(_ArgTypes... ...) _CV _REF> \
98 : _Mem_fn_traits_base<_Res, _CV _Class, _ArgTypes...> \
99 { \
100 using __vararg = true_type; \
101 };
102
103#define _GLIBCXX_MEM_FN_TRAITS(_REF, _LVAL, _RVAL) \
104 _GLIBCXX_MEM_FN_TRAITS2( , _REF, _LVAL, _RVAL) \
105 _GLIBCXX_MEM_FN_TRAITS2(const , _REF, _LVAL, _RVAL) \
106 _GLIBCXX_MEM_FN_TRAITS2(volatile , _REF, _LVAL, _RVAL) \
107 _GLIBCXX_MEM_FN_TRAITS2(const volatile, _REF, _LVAL, _RVAL)
108
109_GLIBCXX_MEM_FN_TRAITS( , true_type, true_type)
110_GLIBCXX_MEM_FN_TRAITS(&, true_type, false_type)
111_GLIBCXX_MEM_FN_TRAITS(&&, false_type, true_type)
112
113#if __cplusplus > 201402L
114_GLIBCXX_MEM_FN_TRAITS(noexcept, true_type, true_type)
115_GLIBCXX_MEM_FN_TRAITS(& noexcept, true_type, false_type)
116_GLIBCXX_MEM_FN_TRAITS(&& noexcept, false_type, true_type)
117#endif
118
119#undef _GLIBCXX_MEM_FN_TRAITS
120#undef _GLIBCXX_MEM_FN_TRAITS2
121
122 /// If we have found a result_type, extract it.
123 template<typename _Functor, typename = __void_t<>>
124 struct _Maybe_get_result_type
125 { };
126
127 template<typename _Functor>
128 struct _Maybe_get_result_type<_Functor,
129 __void_t<typename _Functor::result_type>>
130 { typedef typename _Functor::result_type result_type; };
131
132 /**
133 * Base class for any function object that has a weak result type, as
134 * defined in 20.8.2 [func.require] of C++11.
135 */
136 template<typename _Functor>
137 struct _Weak_result_type_impl
138 : _Maybe_get_result_type<_Functor>
139 { };
140
141 /// Retrieve the result type for a function type.
142 template<typename _Res, typename... _ArgTypes _GLIBCXX_NOEXCEPT_PARM>
143 struct _Weak_result_type_impl<_Res(_ArgTypes...) _GLIBCXX_NOEXCEPT_QUAL>
144 { typedef _Res result_type; };
145
146 /// Retrieve the result type for a varargs function type.
147 template<typename _Res, typename... _ArgTypes _GLIBCXX_NOEXCEPT_PARM>
148 struct _Weak_result_type_impl<_Res(_ArgTypes......) _GLIBCXX_NOEXCEPT_QUAL>
149 { typedef _Res result_type; };
150
151 /// Retrieve the result type for a function pointer.
152 template<typename _Res, typename... _ArgTypes _GLIBCXX_NOEXCEPT_PARM>
153 struct _Weak_result_type_impl<_Res(*)(_ArgTypes...) _GLIBCXX_NOEXCEPT_QUAL>
154 { typedef _Res result_type; };
155
156 /// Retrieve the result type for a varargs function pointer.
157 template<typename _Res, typename... _ArgTypes _GLIBCXX_NOEXCEPT_PARM>
158 struct
159 _Weak_result_type_impl<_Res(*)(_ArgTypes......) _GLIBCXX_NOEXCEPT_QUAL>
160 { typedef _Res result_type; };
161
162 // Let _Weak_result_type_impl perform the real work.
163 template<typename _Functor,
164 bool = is_member_function_pointer<_Functor>::value>
165 struct _Weak_result_type_memfun
166 : _Weak_result_type_impl<_Functor>
167 { };
168
169 // A pointer to member function has a weak result type.
170 template<typename _MemFunPtr>
171 struct _Weak_result_type_memfun<_MemFunPtr, true>
172 {
173 using result_type = typename _Mem_fn_traits<_MemFunPtr>::__result_type;
174 };
175
176 // A pointer to data member doesn't have a weak result type.
177 template<typename _Func, typename _Class>
178 struct _Weak_result_type_memfun<_Func _Class::*, false>
179 { };
180
181 /**
182 * Strip top-level cv-qualifiers from the function object and let
183 * _Weak_result_type_memfun perform the real work.
184 */
185 template<typename _Functor>
186 struct _Weak_result_type
187 : _Weak_result_type_memfun<typename remove_cv<_Functor>::type>
188 { };
189
190#if __cplusplus <= 201703L
191 // Detect nested argument_type.
192 template<typename _Tp, typename = __void_t<>>
193 struct _Refwrap_base_arg1
194 { };
195
196 // Nested argument_type.
197 template<typename _Tp>
198 struct _Refwrap_base_arg1<_Tp,
199 __void_t<typename _Tp::argument_type>>
200 {
201 typedef typename _Tp::argument_type argument_type;
202 };
203
204 // Detect nested first_argument_type and second_argument_type.
205 template<typename _Tp, typename = __void_t<>>
206 struct _Refwrap_base_arg2
207 { };
208
209 // Nested first_argument_type and second_argument_type.
210 template<typename _Tp>
211 struct _Refwrap_base_arg2<_Tp,
212 __void_t<typename _Tp::first_argument_type,
213 typename _Tp::second_argument_type>>
214 {
215 typedef typename _Tp::first_argument_type first_argument_type;
216 typedef typename _Tp::second_argument_type second_argument_type;
217 };
218
219 /**
220 * Derives from unary_function or binary_function when it
221 * can. Specializations handle all of the easy cases. The primary
222 * template determines what to do with a class type, which may
223 * derive from both unary_function and binary_function.
224 */
225 template<typename _Tp>
226 struct _Reference_wrapper_base
227 : _Weak_result_type<_Tp>, _Refwrap_base_arg1<_Tp>, _Refwrap_base_arg2<_Tp>
228 { };
229
230// Ignore warnings about unary_function and binary_function.
231#pragma GCC diagnostic push
232#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
233
234 // - a function type (unary)
235 template<typename _Res, typename _T1 _GLIBCXX_NOEXCEPT_PARM>
236 struct _Reference_wrapper_base<_Res(_T1) _GLIBCXX_NOEXCEPT_QUAL>
237 : unary_function<_T1, _Res>
238 { };
239
240 template<typename _Res, typename _T1>
241 struct _Reference_wrapper_base<_Res(_T1) const>
242 : unary_function<_T1, _Res>
243 { };
244
245 template<typename _Res, typename _T1>
246 struct _Reference_wrapper_base<_Res(_T1) volatile>
247 : unary_function<_T1, _Res>
248 { };
249
250 template<typename _Res, typename _T1>
251 struct _Reference_wrapper_base<_Res(_T1) const volatile>
252 : unary_function<_T1, _Res>
253 { };
254
255 // - a function type (binary)
256 template<typename _Res, typename _T1, typename _T2 _GLIBCXX_NOEXCEPT_PARM>
257 struct _Reference_wrapper_base<_Res(_T1, _T2) _GLIBCXX_NOEXCEPT_QUAL>
258 : binary_function<_T1, _T2, _Res>
259 { };
260
261 template<typename _Res, typename _T1, typename _T2>
262 struct _Reference_wrapper_base<_Res(_T1, _T2) const>
263 : binary_function<_T1, _T2, _Res>
264 { };
265
266 template<typename _Res, typename _T1, typename _T2>
267 struct _Reference_wrapper_base<_Res(_T1, _T2) volatile>
268 : binary_function<_T1, _T2, _Res>
269 { };
270
271 template<typename _Res, typename _T1, typename _T2>
272 struct _Reference_wrapper_base<_Res(_T1, _T2) const volatile>
273 : binary_function<_T1, _T2, _Res>
274 { };
275
276 // - a function pointer type (unary)
277 template<typename _Res, typename _T1 _GLIBCXX_NOEXCEPT_PARM>
278 struct _Reference_wrapper_base<_Res(*)(_T1) _GLIBCXX_NOEXCEPT_QUAL>
279 : unary_function<_T1, _Res>
280 { };
281
282 // - a function pointer type (binary)
283 template<typename _Res, typename _T1, typename _T2 _GLIBCXX_NOEXCEPT_PARM>
284 struct _Reference_wrapper_base<_Res(*)(_T1, _T2) _GLIBCXX_NOEXCEPT_QUAL>
285 : binary_function<_T1, _T2, _Res>
286 { };
287
288 template<typename _Tp, bool = is_member_function_pointer<_Tp>::value>
289 struct _Reference_wrapper_base_memfun
290 : _Reference_wrapper_base<_Tp>
291 { };
292
293 template<typename _MemFunPtr>
294 struct _Reference_wrapper_base_memfun<_MemFunPtr, true>
295 : _Mem_fn_traits<_MemFunPtr>::__maybe_type
296 {
297 using result_type = typename _Mem_fn_traits<_MemFunPtr>::__result_type;
298 };
299#pragma GCC diagnostic pop
300#endif // ! C++20
301
302 /// @endcond
303
304 /**
305 * @brief Primary class template for reference_wrapper.
306 * @ingroup functors
307 */
308 template<typename _Tp>
310#if __cplusplus <= 201703L
311 // In C++20 std::reference_wrapper<T> allows T to be incomplete,
312 // so checking for nested types could result in ODR violations.
313 : public _Reference_wrapper_base_memfun<typename remove_cv<_Tp>::type>
314#endif
315 {
316 _Tp* _M_data;
317
318 _GLIBCXX20_CONSTEXPR
319 static _Tp* _S_fun(_Tp& __r) noexcept { return std::__addressof(__r); }
320
321 static void _S_fun(_Tp&&) = delete;
322
323 template<typename _Up, typename _Up2 = __remove_cvref_t<_Up>>
324 using __not_same
326
327 public:
328 typedef _Tp type;
329
330 // _GLIBCXX_RESOLVE_LIB_DEFECTS
331 // 2993. reference_wrapper<T> conversion from T&&
332 // 3041. Unnecessary decay in reference_wrapper
333 template<typename _Up, typename = __not_same<_Up>, typename
334 = decltype(reference_wrapper::_S_fun(std::declval<_Up>()))>
335 _GLIBCXX20_CONSTEXPR
336 reference_wrapper(_Up&& __uref)
337 noexcept(noexcept(reference_wrapper::_S_fun(std::declval<_Up>())))
338 : _M_data(reference_wrapper::_S_fun(std::forward<_Up>(__uref)))
339 { }
340
341 reference_wrapper(const reference_wrapper&) = default;
342
344 operator=(const reference_wrapper&) = default;
345
346 _GLIBCXX20_CONSTEXPR
347 operator _Tp&() const noexcept
348 { return this->get(); }
349
350 _GLIBCXX20_CONSTEXPR
351 _Tp&
352 get() const noexcept
353 { return *_M_data; }
354
355 template<typename... _Args>
356 _GLIBCXX20_CONSTEXPR
357 typename __invoke_result<_Tp&, _Args...>::type
358 operator()(_Args&&... __args) const
359 noexcept(__is_nothrow_invocable<_Tp&, _Args...>::value)
360 {
361#if __cplusplus > 201703L
362 if constexpr (is_object_v<type>)
363 static_assert(sizeof(type), "type must be complete");
364#endif
365 return std::__invoke(get(), std::forward<_Args>(__args)...);
366 }
367
368#if __glibcxx_reference_wrapper >= 202403L // >= C++26
369 // [refwrap.comparisons], comparisons
370 [[nodiscard]]
371 friend constexpr bool
372 operator==(reference_wrapper __x, reference_wrapper __y)
373 requires requires { { __x.get() == __y.get() } -> convertible_to<bool>; }
374 { return __x.get() == __y.get(); }
375
376 [[nodiscard]]
377 friend constexpr bool
378 operator==(reference_wrapper __x, const _Tp& __y)
379 requires requires { { __x.get() == __y } -> convertible_to<bool>; }
380 { return __x.get() == __y; }
381
382 [[nodiscard]]
383 friend constexpr bool
385 requires (!is_const_v<_Tp>)
386 && requires { { __x.get() == __y.get() } -> convertible_to<bool>; }
387 { return __x.get() == __y.get(); }
388
389 // _GLIBCXX_RESOLVE_LIB_DEFECTS
390 // 4071. reference_wrapper comparisons are not SFINAE-friendly
391
392 [[nodiscard]]
393 friend constexpr auto
394 operator<=>(reference_wrapper __x, reference_wrapper __y)
395 requires requires (const _Tp __t) {
396 { __t < __t } -> __detail::__boolean_testable;
397 }
398 { return __detail::__synth3way(__x.get(), __y.get()); }
399
400 [[nodiscard]]
401 friend constexpr auto
402 operator<=>(reference_wrapper __x, const _Tp& __y)
403 requires requires { { __y < __y } -> __detail::__boolean_testable; }
404 { return __detail::__synth3way(__x.get(), __y); }
405
406 [[nodiscard]]
407 friend constexpr auto
409 requires (!is_const_v<_Tp>) && requires (const _Tp __t) {
410 { __t < __t } -> __detail::__boolean_testable;
411 }
412 { return __detail::__synth3way(__x.get(), __y.get()); }
413#endif
414 };
415
416#if __cpp_deduction_guides
417 template<typename _Tp>
419#endif
420
421 /// @relates reference_wrapper @{
422
423 /// Denotes a reference should be taken to a variable.
424 template<typename _Tp>
425 _GLIBCXX20_CONSTEXPR
427 ref(_Tp& __t) noexcept
428 { return reference_wrapper<_Tp>(__t); }
429
430 /// Denotes a const reference should be taken to a variable.
431 template<typename _Tp>
432 _GLIBCXX20_CONSTEXPR
434 cref(const _Tp& __t) noexcept
435 { return reference_wrapper<const _Tp>(__t); }
436
437 template<typename _Tp>
438 void ref(const _Tp&&) = delete;
439
440 template<typename _Tp>
441 void cref(const _Tp&&) = delete;
442
443 /// std::ref overload to prevent wrapping a reference_wrapper
444 template<typename _Tp>
445 _GLIBCXX20_CONSTEXPR
448 { return __t; }
449
450 /// std::cref overload to prevent wrapping a reference_wrapper
451 template<typename _Tp>
452 _GLIBCXX20_CONSTEXPR
455 { return { __t.get() }; }
456
457 /// @}
458
459_GLIBCXX_END_NAMESPACE_VERSION
460} // namespace std
461
462#endif // C++11
463
464#endif // _GLIBCXX_REFWRAP_H
__bool_constant< true > true_type
The type used as a compile-time boolean with true value.
Definition type_traits:116
__bool_constant< false > false_type
The type used as a compile-time boolean with false value.
Definition type_traits:119
constexpr __invoke_result< _Callable, _Args... >::type __invoke(_Callable &&__fn, _Args &&... __args) noexcept(__is_nothrow_invocable< _Callable, _Args... >::value)
Invoke a callable object.
Definition invoke.h:92
constexpr _Tp * __addressof(_Tp &__r) noexcept
Same as C++11 std::addressof.
Definition move.h:51
ISO C++ entities toplevel namespace is std.
Primary class template for reference_wrapper.
Definition refwrap.h:315
constexpr reference_wrapper< _Tp > ref(_Tp &__t) noexcept
Definition refwrap.h:427
constexpr reference_wrapper< _Tp > ref(reference_wrapper< _Tp > __t) noexcept
std::ref overload to prevent wrapping a reference_wrapper
Definition refwrap.h:447
constexpr reference_wrapper< const _Tp > cref(const _Tp &__t) noexcept
Denotes a const reference should be taken to a variable.
Definition refwrap.h:434
constexpr reference_wrapper< const _Tp > cref(reference_wrapper< _Tp > __t) noexcept
std::cref overload to prevent wrapping a reference_wrapper
Definition refwrap.h:454
Define a member typedef type only if a boolean constant is true.
Definition type_traits:134