xref: /freebsd/contrib/llvm-project/libcxx/include/__functional/reference_wrapper.h (revision e64bea71c21eb42e97aa615188ba91f6cce0d36d)
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 
37 template <class _Tp>
38 class reference_wrapper : public __weak_result_type<_Tp> {
39 public:
40   // types
41   typedef _Tp type;
42 
43 private:
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 
49 public:
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_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reference_wrapper(_Up&& __u)
54       _NOEXCEPT_(noexcept(__fun(std::declval<_Up>()))) {
55     type& __f = static_cast<_Up&&>(__u);
56     __f_      = std::addressof(__f);
57   }
58 
59   // access
60   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 operator type&() const _NOEXCEPT { return *__f_; }
61   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 type& get() const _NOEXCEPT { return *__f_; }
62 
63   // invoke
64   template <class... _ArgTypes>
65   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __invoke_result_t<type&, _ArgTypes...>
66   operator()(_ArgTypes&&... __args) const
67 #if _LIBCPP_STD_VER >= 17
68       // Since is_nothrow_invocable requires C++17 LWG3764 is not backported
69       // to earlier versions.
70       noexcept(is_nothrow_invocable_v<_Tp&, _ArgTypes...>)
71 #endif
72   {
73     return std::__invoke(get(), std::forward<_ArgTypes>(__args)...);
74   }
75 
76 #if _LIBCPP_STD_VER >= 26
77 
78   // [refwrap.comparisons], comparisons
79 
80   _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(reference_wrapper __x, reference_wrapper __y)
81     requires requires {
82       { __x.get() == __y.get() } -> __core_convertible_to<bool>;
83     }
84   {
85     return __x.get() == __y.get();
86   }
87 
88   _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(reference_wrapper __x, const _Tp& __y)
89     requires requires {
90       { __x.get() == __y } -> __core_convertible_to<bool>;
91     }
92   {
93     return __x.get() == __y;
94   }
95 
96   _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(reference_wrapper __x, reference_wrapper<const _Tp> __y)
97     requires(!is_const_v<_Tp>) && requires {
98       { __x.get() == __y.get() } -> __core_convertible_to<bool>;
99     }
100   {
101     return __x.get() == __y.get();
102   }
103 
104   _LIBCPP_HIDE_FROM_ABI friend constexpr auto operator<=>(reference_wrapper __x, reference_wrapper __y)
105     requires requires { std::__synth_three_way(__x.get(), __y.get()); }
106   {
107     return std::__synth_three_way(__x.get(), __y.get());
108   }
109 
110   _LIBCPP_HIDE_FROM_ABI friend constexpr auto operator<=>(reference_wrapper __x, const _Tp& __y)
111     requires requires { std::__synth_three_way(__x.get(), __y); }
112   {
113     return std::__synth_three_way(__x.get(), __y);
114   }
115 
116   _LIBCPP_HIDE_FROM_ABI friend constexpr auto operator<=>(reference_wrapper __x, reference_wrapper<const _Tp> __y)
117     requires(!is_const_v<_Tp>) && requires { std::__synth_three_way(__x.get(), __y.get()); }
118   {
119     return std::__synth_three_way(__x.get(), __y.get());
120   }
121 
122 #endif // _LIBCPP_STD_VER >= 26
123 };
124 
125 #if _LIBCPP_STD_VER >= 17
126 template <class _Tp>
127 reference_wrapper(_Tp&) -> reference_wrapper<_Tp>;
128 #endif
129 
130 template <class _Tp>
131 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reference_wrapper<_Tp> ref(_Tp& __t) _NOEXCEPT {
132   return reference_wrapper<_Tp>(__t);
133 }
134 
135 template <class _Tp>
136 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reference_wrapper<_Tp>
137 ref(reference_wrapper<_Tp> __t) _NOEXCEPT {
138   return __t;
139 }
140 
141 template <class _Tp>
142 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reference_wrapper<const _Tp> cref(const _Tp& __t) _NOEXCEPT {
143   return reference_wrapper<const _Tp>(__t);
144 }
145 
146 template <class _Tp>
147 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reference_wrapper<const _Tp>
148 cref(reference_wrapper<_Tp> __t) _NOEXCEPT {
149   return __t;
150 }
151 
152 template <class _Tp>
153 void ref(const _Tp&&) = delete;
154 template <class _Tp>
155 void cref(const _Tp&&) = delete;
156 
157 // Let desugars-to pass through std::reference_wrapper
158 template <class _CanonicalTag, class _Operation, class... _Args>
159 inline const bool __desugars_to_v<_CanonicalTag, reference_wrapper<_Operation>, _Args...> =
160     __desugars_to_v<_CanonicalTag, _Operation, _Args...>;
161 
162 #if _LIBCPP_STD_VER >= 20
163 
164 template <class _Tp>
165 inline constexpr bool __is_ref_wrapper = __is_specialization_v<_Tp, reference_wrapper>;
166 
167 template <class _Rp, class _Tp, class _RpQual, class _TpQual>
168 concept __ref_wrap_common_reference_exists_with = __is_ref_wrapper<_Rp> && requires {
169   typename common_reference_t<typename _Rp::type&, _TpQual>;
170 } && convertible_to<_RpQual, common_reference_t<typename _Rp::type&, _TpQual>>;
171 
172 template <class _Rp, class _Tp, template <class> class _RpQual, template <class> class _TpQual>
173   requires(__ref_wrap_common_reference_exists_with<_Rp, _Tp, _RpQual<_Rp>, _TpQual<_Tp>> &&
174            !__ref_wrap_common_reference_exists_with<_Tp, _Rp, _TpQual<_Tp>, _RpQual<_Rp>>)
175 struct basic_common_reference<_Rp, _Tp, _RpQual, _TpQual> {
176   using type _LIBCPP_NODEBUG = common_reference_t<typename _Rp::type&, _TpQual<_Tp>>;
177 };
178 
179 template <class _Tp, class _Rp, template <class> class _TpQual, template <class> class _RpQual>
180   requires(__ref_wrap_common_reference_exists_with<_Rp, _Tp, _RpQual<_Rp>, _TpQual<_Tp>> &&
181            !__ref_wrap_common_reference_exists_with<_Tp, _Rp, _TpQual<_Tp>, _RpQual<_Rp>>)
182 struct basic_common_reference<_Tp, _Rp, _TpQual, _RpQual> {
183   using type _LIBCPP_NODEBUG = common_reference_t<typename _Rp::type&, _TpQual<_Tp>>;
184 };
185 
186 #endif // _LIBCPP_STD_VER >= 20
187 
188 _LIBCPP_END_NAMESPACE_STD
189 
190 #endif // _LIBCPP___FUNCTIONAL_REFERENCE_WRAPPER_H
191