1// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP___FUNCTIONAL_REFERENCE_WRAPPER_H
11#define _LIBCPP___FUNCTIONAL_REFERENCE_WRAPPER_H
12
13#include <__compare/synth_three_way.h>
14#include <__concepts/convertible_to.h>
15#include <__config>
16#include <__functional/weak_result_type.h>
17#include <__memory/addressof.h>
18#include <__type_traits/common_reference.h>
19#include <__type_traits/desugars_to.h>
20#include <__type_traits/enable_if.h>
21#include <__type_traits/invoke.h>
22#include <__type_traits/is_const.h>
23#include <__type_traits/is_core_convertible.h>
24#include <__type_traits/is_same.h>
25#include <__type_traits/is_specialization.h>
26#include <__type_traits/remove_cvref.h>
27#include <__type_traits/void_t.h>
28#include <__utility/declval.h>
29#include <__utility/forward.h>
30
31#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
32# pragma GCC system_header
33#endif
34
35_LIBCPP_BEGIN_NAMESPACE_STD
36
37template <class _Tp>
38class reference_wrapper : public __weak_result_type<_Tp> {
39public:
40 // types
41 typedef _Tp type;
42
43private:
44 type* __f_;
45
46 static void __fun(_Tp&) _NOEXCEPT;
47 static void __fun(_Tp&&) = delete; // NOLINT(modernize-use-equals-delete) ; This is llvm.org/PR54276
48
49public:
50 template <class _Up,
51 class = __void_t<decltype(__fun(std::declval<_Up>()))>,
52 __enable_if_t<!is_same<__remove_cvref_t<_Up>, reference_wrapper>::value, int> = 0>
53 _LIBCPP_CONSTEXPR_SINCE_CXX20 reference_wrapper(_Up&& __u) _NOEXCEPT_(noexcept(__fun(std::declval<_Up>()))) {
54 type& __f = static_cast<_Up&&>(__u);
55 __f_ = std::addressof(__f);
56 }
57
58 // access
59 _LIBCPP_CONSTEXPR_SINCE_CXX20 operator type&() const _NOEXCEPT { return *__f_; }
60 [[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX20 type& get() const _NOEXCEPT { return *__f_; }
61
62 // invoke
63 template <class... _ArgTypes>
64 _LIBCPP_CONSTEXPR_SINCE_CXX20 __direct_invoke_result_t<type&, _ArgTypes...> operator()(_ArgTypes&&... __args) const
65#if _LIBCPP_STD_VER >= 17
66 // Since is_nothrow_invocable requires C++17 LWG3764 is not backported
67 // to earlier versions.
68 noexcept(is_nothrow_invocable_v<_Tp&, _ArgTypes...>)
69#endif
70 {
71 return std::__invoke(get(), std::forward<_ArgTypes>(__args)...);
72 }
73
74#if _LIBCPP_STD_VER >= 26
75
76 // [refwrap.comparisons], comparisons
77
78 friend constexpr bool operator==(reference_wrapper __x, reference_wrapper __y)
79 requires requires {
80 { __x.get() == __y.get() } -> __core_convertible_to<bool>;
81 }
82 {
83 return __x.get() == __y.get();
84 }
85
86 friend constexpr bool operator==(reference_wrapper __x, const _Tp& __y)
87 requires requires {
88 { __x.get() == __y } -> __core_convertible_to<bool>;
89 }
90 {
91 return __x.get() == __y;
92 }
93
94 friend constexpr bool operator==(reference_wrapper __x, reference_wrapper<const _Tp> __y)
95 requires(!is_const_v<_Tp>) && requires {
96 { __x.get() == __y.get() } -> __core_convertible_to<bool>;
97 }
98 {
99 return __x.get() == __y.get();
100 }
101
102 friend constexpr auto operator<=>(reference_wrapper __x, reference_wrapper __y)
103 requires requires { std::__synth_three_way(__x.get(), __y.get()); }
104 {
105 return std::__synth_three_way(__x.get(), __y.get());
106 }
107
108 friend constexpr auto operator<=>(reference_wrapper __x, const _Tp& __y)
109 requires requires { std::__synth_three_way(__x.get(), __y); }
110 {
111 return std::__synth_three_way(__x.get(), __y);
112 }
113
114 friend constexpr auto operator<=>(reference_wrapper __x, reference_wrapper<const _Tp> __y)
115 requires(!is_const_v<_Tp>) && requires { std::__synth_three_way(__x.get(), __y.get()); }
116 {
117 return std::__synth_three_way(__x.get(), __y.get());
118 }
119
120#endif // _LIBCPP_STD_VER >= 26
121};
122
123#if _LIBCPP_STD_VER >= 17
124template <class _Tp>
125reference_wrapper(_Tp&) -> reference_wrapper<_Tp>;
126#endif
127
128template <class _Tp>
129[[__nodiscard__]] inline _LIBCPP_CONSTEXPR_SINCE_CXX20 reference_wrapper<_Tp> ref(_Tp& __t) _NOEXCEPT {
130 return reference_wrapper<_Tp>(__t);
131}
132
133template <class _Tp>
134[[__nodiscard__]] inline _LIBCPP_CONSTEXPR_SINCE_CXX20 reference_wrapper<_Tp>
135ref(reference_wrapper<_Tp> __t) _NOEXCEPT {
136 return __t;
137}
138
139template <class _Tp>
140[[__nodiscard__]] inline _LIBCPP_CONSTEXPR_SINCE_CXX20 reference_wrapper<const _Tp> cref(const _Tp& __t) _NOEXCEPT {
141 return reference_wrapper<const _Tp>(__t);
142}
143
144template <class _Tp>
145[[__nodiscard__]] inline _LIBCPP_CONSTEXPR_SINCE_CXX20 reference_wrapper<const _Tp>
146cref(reference_wrapper<_Tp> __t) _NOEXCEPT {
147 return __t;
148}
149
150template <class _Tp>
151void ref(const _Tp&&) = delete;
152template <class _Tp>
153void cref(const _Tp&&) = delete;
154
155// Let desugars-to pass through std::reference_wrapper
156template <class _CanonicalTag, class _Operation, class... _Args>
157inline const bool __desugars_to_v<_CanonicalTag, reference_wrapper<_Operation>, _Args...> =
158 __desugars_to_v<_CanonicalTag, _Operation, _Args...>;
159
160#if _LIBCPP_STD_VER >= 20
161
162template <class _Tp>
163inline constexpr bool __is_ref_wrapper = __is_specialization_v<_Tp, reference_wrapper>;
164
165template <class _Rp, class _Tp, class _RpQual, class _TpQual>
166concept __ref_wrap_common_reference_exists_with = __is_ref_wrapper<_Rp> && requires {
167 typename common_reference_t<typename _Rp::type&, _TpQual>;
168} && convertible_to<_RpQual, common_reference_t<typename _Rp::type&, _TpQual>>;
169
170template <class _Rp, class _Tp, template <class> class _RpQual, template <class> class _TpQual>
171 requires(__ref_wrap_common_reference_exists_with<_Rp, _Tp, _RpQual<_Rp>, _TpQual<_Tp>> &&
172 !__ref_wrap_common_reference_exists_with<_Tp, _Rp, _TpQual<_Tp>, _RpQual<_Rp>>)
173struct basic_common_reference<_Rp, _Tp, _RpQual, _TpQual> {
174 using type _LIBCPP_NODEBUG = common_reference_t<typename _Rp::type&, _TpQual<_Tp>>;
175};
176
177template <class _Tp, class _Rp, template <class> class _TpQual, template <class> class _RpQual>
178 requires(__ref_wrap_common_reference_exists_with<_Rp, _Tp, _RpQual<_Rp>, _TpQual<_Tp>> &&
179 !__ref_wrap_common_reference_exists_with<_Tp, _Rp, _TpQual<_Tp>, _RpQual<_Rp>>)
180struct basic_common_reference<_Tp, _Rp, _TpQual, _RpQual> {
181 using type _LIBCPP_NODEBUG = common_reference_t<typename _Rp::type&, _TpQual<_Tp>>;
182};
183
184#endif // _LIBCPP_STD_VER >= 20
185
186_LIBCPP_END_NAMESPACE_STD
187
188#endif // _LIBCPP___FUNCTIONAL_REFERENCE_WRAPPER_H
189