193 lines · c
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-exception7//8//===----------------------------------------------------------------------===//9 10#ifndef _LIBCPP___FUNCTIONAL_REFERENCE_WRAPPER_H11#define _LIBCPP___FUNCTIONAL_REFERENCE_WRAPPER_H12 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_header33#endif34 35_LIBCPP_BEGIN_NAMESPACE_STD36 37template <class _Tp>38class reference_wrapper : public __weak_result_type<_Tp> {39public:40 // types41 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/PR5427648 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_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 // access60 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 operator type&() const _NOEXCEPT { return *__f_; }61 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 type& get() const _NOEXCEPT { return *__f_; }62 63 // invoke64 template <class... _ArgTypes>65 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __invoke_result_t<type&, _ArgTypes...>66 operator()(_ArgTypes&&... __args) const67#if _LIBCPP_STD_VER >= 1768 // Since is_nothrow_invocable requires C++17 LWG3764 is not backported69 // to earlier versions.70 noexcept(is_nothrow_invocable_v<_Tp&, _ArgTypes...>)71#endif72 {73 return std::__invoke(get(), std::forward<_ArgTypes>(__args)...);74 }75 76#if _LIBCPP_STD_VER >= 2677 78 // [refwrap.comparisons], comparisons79 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 >= 26123};124 125#if _LIBCPP_STD_VER >= 17126template <class _Tp>127reference_wrapper(_Tp&) -> reference_wrapper<_Tp>;128#endif129 130template <class _Tp>131[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI132_LIBCPP_CONSTEXPR_SINCE_CXX20 reference_wrapper<_Tp> ref(_Tp& __t) _NOEXCEPT {133 return reference_wrapper<_Tp>(__t);134}135 136template <class _Tp>137[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reference_wrapper<_Tp>138ref(reference_wrapper<_Tp> __t) _NOEXCEPT {139 return __t;140}141 142template <class _Tp>143[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reference_wrapper<const _Tp>144cref(const _Tp& __t) _NOEXCEPT {145 return reference_wrapper<const _Tp>(__t);146}147 148template <class _Tp>149[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reference_wrapper<const _Tp>150cref(reference_wrapper<_Tp> __t) _NOEXCEPT {151 return __t;152}153 154template <class _Tp>155void ref(const _Tp&&) = delete;156template <class _Tp>157void cref(const _Tp&&) = delete;158 159// Let desugars-to pass through std::reference_wrapper160template <class _CanonicalTag, class _Operation, class... _Args>161inline const bool __desugars_to_v<_CanonicalTag, reference_wrapper<_Operation>, _Args...> =162 __desugars_to_v<_CanonicalTag, _Operation, _Args...>;163 164#if _LIBCPP_STD_VER >= 20165 166template <class _Tp>167inline constexpr bool __is_ref_wrapper = __is_specialization_v<_Tp, reference_wrapper>;168 169template <class _Rp, class _Tp, class _RpQual, class _TpQual>170concept __ref_wrap_common_reference_exists_with = __is_ref_wrapper<_Rp> && requires {171 typename common_reference_t<typename _Rp::type&, _TpQual>;172} && convertible_to<_RpQual, common_reference_t<typename _Rp::type&, _TpQual>>;173 174template <class _Rp, class _Tp, template <class> class _RpQual, template <class> class _TpQual>175 requires(__ref_wrap_common_reference_exists_with<_Rp, _Tp, _RpQual<_Rp>, _TpQual<_Tp>> &&176 !__ref_wrap_common_reference_exists_with<_Tp, _Rp, _TpQual<_Tp>, _RpQual<_Rp>>)177struct basic_common_reference<_Rp, _Tp, _RpQual, _TpQual> {178 using type _LIBCPP_NODEBUG = common_reference_t<typename _Rp::type&, _TpQual<_Tp>>;179};180 181template <class _Tp, class _Rp, template <class> class _TpQual, template <class> class _RpQual>182 requires(__ref_wrap_common_reference_exists_with<_Rp, _Tp, _RpQual<_Rp>, _TpQual<_Tp>> &&183 !__ref_wrap_common_reference_exists_with<_Tp, _Rp, _TpQual<_Tp>, _RpQual<_Rp>>)184struct basic_common_reference<_Tp, _Rp, _TpQual, _RpQual> {185 using type _LIBCPP_NODEBUG = common_reference_t<typename _Rp::type&, _TpQual<_Tp>>;186};187 188#endif // _LIBCPP_STD_VER >= 20189 190_LIBCPP_END_NAMESPACE_STD191 192#endif // _LIBCPP___FUNCTIONAL_REFERENCE_WRAPPER_H193