1fe6060f1SDimitry Andric // -*- C++ -*- 2fe6060f1SDimitry Andric //===----------------------------------------------------------------------===// 3fe6060f1SDimitry Andric // 4fe6060f1SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5fe6060f1SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 6fe6060f1SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7fe6060f1SDimitry Andric // 8fe6060f1SDimitry Andric //===----------------------------------------------------------------------===// 9fe6060f1SDimitry Andric 10fe6060f1SDimitry Andric #ifndef _LIBCPP___FUNCTIONAL_REFERENCE_WRAPPER_H 11fe6060f1SDimitry Andric #define _LIBCPP___FUNCTIONAL_REFERENCE_WRAPPER_H 12fe6060f1SDimitry Andric 13*0fca6ea1SDimitry Andric #include <__compare/synth_three_way.h> 14*0fca6ea1SDimitry Andric #include <__concepts/boolean_testable.h> 15fe6060f1SDimitry Andric #include <__config> 16bdd1243dSDimitry Andric #include <__functional/invoke.h> 17fe6060f1SDimitry Andric #include <__functional/weak_result_type.h> 18fe6060f1SDimitry Andric #include <__memory/addressof.h> 19bdd1243dSDimitry Andric #include <__type_traits/enable_if.h> 20*0fca6ea1SDimitry Andric #include <__type_traits/is_const.h> 21bdd1243dSDimitry Andric #include <__type_traits/remove_cvref.h> 22*0fca6ea1SDimitry Andric #include <__type_traits/void_t.h> 23bdd1243dSDimitry Andric #include <__utility/declval.h> 24fe6060f1SDimitry Andric #include <__utility/forward.h> 25fe6060f1SDimitry Andric 26fe6060f1SDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 27fe6060f1SDimitry Andric # pragma GCC system_header 28fe6060f1SDimitry Andric #endif 29fe6060f1SDimitry Andric 30fe6060f1SDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD 31fe6060f1SDimitry Andric 32fe6060f1SDimitry Andric template <class _Tp> 33cb14a3feSDimitry Andric class _LIBCPP_TEMPLATE_VIS reference_wrapper : public __weak_result_type<_Tp> { 34fe6060f1SDimitry Andric public: 35fe6060f1SDimitry Andric // types 36fe6060f1SDimitry Andric typedef _Tp type; 37cb14a3feSDimitry Andric 38fe6060f1SDimitry Andric private: 39fe6060f1SDimitry Andric type* __f_; 40fe6060f1SDimitry Andric 41fe6060f1SDimitry Andric static void __fun(_Tp&) _NOEXCEPT; 42*0fca6ea1SDimitry Andric static void __fun(_Tp&&) = delete; // NOLINT(modernize-use-equals-delete) ; This is llvm.org/PR54276 43fe6060f1SDimitry Andric 44fe6060f1SDimitry Andric public: 45*0fca6ea1SDimitry Andric template <class _Up, 46*0fca6ea1SDimitry Andric class = __void_t<decltype(__fun(std::declval<_Up>()))>, 47*0fca6ea1SDimitry Andric __enable_if_t<!__is_same_uncvref<_Up, reference_wrapper>::value, int> = 0> 48cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reference_wrapper(_Up&& __u) 49cb14a3feSDimitry Andric _NOEXCEPT_(noexcept(__fun(std::declval<_Up>()))) { 50fe6060f1SDimitry Andric type& __f = static_cast<_Up&&>(__u); 515f757f3fSDimitry Andric __f_ = std::addressof(__f); 52fe6060f1SDimitry Andric } 53fe6060f1SDimitry Andric 54fe6060f1SDimitry Andric // access 55cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 operator type&() const _NOEXCEPT { return *__f_; } 56cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 type& get() const _NOEXCEPT { return *__f_; } 57fe6060f1SDimitry Andric 58fe6060f1SDimitry Andric // invoke 59fe6060f1SDimitry Andric template <class... _ArgTypes> 60cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 typename __invoke_of<type&, _ArgTypes...>::type 6106c3fb27SDimitry Andric operator()(_ArgTypes&&... __args) const 6206c3fb27SDimitry Andric #if _LIBCPP_STD_VER >= 17 6306c3fb27SDimitry Andric // Since is_nothrow_invocable requires C++17 LWG3764 is not backported 6406c3fb27SDimitry Andric // to earlier versions. 6506c3fb27SDimitry Andric noexcept(is_nothrow_invocable_v<_Tp&, _ArgTypes...>) 6606c3fb27SDimitry Andric #endif 6706c3fb27SDimitry Andric { 6881ad6265SDimitry Andric return std::__invoke(get(), std::forward<_ArgTypes>(__args)...); 69fe6060f1SDimitry Andric } 70*0fca6ea1SDimitry Andric 71*0fca6ea1SDimitry Andric #if _LIBCPP_STD_VER >= 26 72*0fca6ea1SDimitry Andric 73*0fca6ea1SDimitry Andric // [refwrap.comparisons], comparisons 74*0fca6ea1SDimitry Andric 75*0fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(reference_wrapper __x, reference_wrapper __y) 76*0fca6ea1SDimitry Andric requires requires { 77*0fca6ea1SDimitry Andric { __x.get() == __y.get() } -> __boolean_testable; 78*0fca6ea1SDimitry Andric } 79*0fca6ea1SDimitry Andric { 80*0fca6ea1SDimitry Andric return __x.get() == __y.get(); 81*0fca6ea1SDimitry Andric } 82*0fca6ea1SDimitry Andric 83*0fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(reference_wrapper __x, const _Tp& __y) 84*0fca6ea1SDimitry Andric requires requires { 85*0fca6ea1SDimitry Andric { __x.get() == __y } -> __boolean_testable; 86*0fca6ea1SDimitry Andric } 87*0fca6ea1SDimitry Andric { 88*0fca6ea1SDimitry Andric return __x.get() == __y; 89*0fca6ea1SDimitry Andric } 90*0fca6ea1SDimitry Andric 91*0fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(reference_wrapper __x, reference_wrapper<const _Tp> __y) 92*0fca6ea1SDimitry Andric requires(!is_const_v<_Tp>) && requires { 93*0fca6ea1SDimitry Andric { __x.get() == __y.get() } -> __boolean_testable; 94*0fca6ea1SDimitry Andric } 95*0fca6ea1SDimitry Andric { 96*0fca6ea1SDimitry Andric return __x.get() == __y.get(); 97*0fca6ea1SDimitry Andric } 98*0fca6ea1SDimitry Andric 99*0fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI friend constexpr auto operator<=>(reference_wrapper __x, reference_wrapper __y) 100*0fca6ea1SDimitry Andric requires requires { std::__synth_three_way(__x.get(), __y.get()); } 101*0fca6ea1SDimitry Andric { 102*0fca6ea1SDimitry Andric return std::__synth_three_way(__x.get(), __y.get()); 103*0fca6ea1SDimitry Andric } 104*0fca6ea1SDimitry Andric 105*0fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI friend constexpr auto operator<=>(reference_wrapper __x, const _Tp& __y) 106*0fca6ea1SDimitry Andric requires requires { std::__synth_three_way(__x.get(), __y); } 107*0fca6ea1SDimitry Andric { 108*0fca6ea1SDimitry Andric return std::__synth_three_way(__x.get(), __y); 109*0fca6ea1SDimitry Andric } 110*0fca6ea1SDimitry Andric 111*0fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI friend constexpr auto operator<=>(reference_wrapper __x, reference_wrapper<const _Tp> __y) 112*0fca6ea1SDimitry Andric requires(!is_const_v<_Tp>) && requires { std::__synth_three_way(__x.get(), __y.get()); } 113*0fca6ea1SDimitry Andric { 114*0fca6ea1SDimitry Andric return std::__synth_three_way(__x.get(), __y.get()); 115*0fca6ea1SDimitry Andric } 116*0fca6ea1SDimitry Andric 117*0fca6ea1SDimitry Andric #endif // _LIBCPP_STD_VER >= 26 118fe6060f1SDimitry Andric }; 119fe6060f1SDimitry Andric 12006c3fb27SDimitry Andric #if _LIBCPP_STD_VER >= 17 121fe6060f1SDimitry Andric template <class _Tp> 122fe6060f1SDimitry Andric reference_wrapper(_Tp&) -> reference_wrapper<_Tp>; 123fe6060f1SDimitry Andric #endif 124fe6060f1SDimitry Andric 125fe6060f1SDimitry Andric template <class _Tp> 126cb14a3feSDimitry Andric inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reference_wrapper<_Tp> ref(_Tp& __t) _NOEXCEPT { 127fe6060f1SDimitry Andric return reference_wrapper<_Tp>(__t); 128fe6060f1SDimitry Andric } 129fe6060f1SDimitry Andric 130fe6060f1SDimitry Andric template <class _Tp> 131cb14a3feSDimitry Andric inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reference_wrapper<_Tp> 132cb14a3feSDimitry Andric ref(reference_wrapper<_Tp> __t) _NOEXCEPT { 13304eeddc0SDimitry Andric return __t; 134fe6060f1SDimitry Andric } 135fe6060f1SDimitry Andric 136fe6060f1SDimitry Andric template <class _Tp> 137cb14a3feSDimitry Andric inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reference_wrapper<const _Tp> cref(const _Tp& __t) _NOEXCEPT { 138fe6060f1SDimitry Andric return reference_wrapper<const _Tp>(__t); 139fe6060f1SDimitry Andric } 140fe6060f1SDimitry Andric 141fe6060f1SDimitry Andric template <class _Tp> 142cb14a3feSDimitry Andric inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reference_wrapper<const _Tp> 143cb14a3feSDimitry Andric cref(reference_wrapper<_Tp> __t) _NOEXCEPT { 14404eeddc0SDimitry Andric return __t; 145fe6060f1SDimitry Andric } 146fe6060f1SDimitry Andric 147cb14a3feSDimitry Andric template <class _Tp> 148cb14a3feSDimitry Andric void ref(const _Tp&&) = delete; 149cb14a3feSDimitry Andric template <class _Tp> 150cb14a3feSDimitry Andric void cref(const _Tp&&) = delete; 151fe6060f1SDimitry Andric 152fe6060f1SDimitry Andric _LIBCPP_END_NAMESPACE_STD 153fe6060f1SDimitry Andric 154fe6060f1SDimitry Andric #endif // _LIBCPP___FUNCTIONAL_REFERENCE_WRAPPER_H 155