2 //===----------------------------------------------------------------------===//
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
8 //===----------------------------------------------------------------------===//
10 #ifndef _LIBCPP___FUNCTIONAL_REFERENCE_WRAPPER_H
11 #define _LIBCPP___FUNCTIONAL_REFERENCE_WRAPPER_H
14 #include <__functional/invoke.h>
15 #include <__functional/weak_result_type.h>
16 #include <__memory/addressof.h>
17 #include <__type_traits/enable_if.h>
18 #include <__type_traits/remove_cvref.h>
19 #include <__utility/declval.h>
20 #include <__utility/forward.h>
22 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
23 # pragma GCC system_header
26 _LIBCPP_BEGIN_NAMESPACE_STD
29 class _LIBCPP_TEMPLATE_VIS reference_wrapper : public __weak_result_type<_Tp>
37 static void __fun(_Tp&) _NOEXCEPT;
38 static void __fun(_Tp&&) = delete;
41 template <class _Up, class = __enable_if_t<!__is_same_uncvref<_Up, reference_wrapper>::value, decltype(__fun(std::declval<_Up>())) > >
42 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
43 reference_wrapper(_Up&& __u) _NOEXCEPT_(noexcept(__fun(std::declval<_Up>()))) {
44 type& __f = static_cast<_Up&&>(__u);
45 __f_ = _VSTD::addressof(__f);
49 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
50 operator type&() const _NOEXCEPT {return *__f_;}
51 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
52 type& get() const _NOEXCEPT {return *__f_;}
55 template <class... _ArgTypes>
56 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
57 typename __invoke_of<type&, _ArgTypes...>::type
58 operator() (_ArgTypes&&... __args) const {
59 return std::__invoke(get(), std::forward<_ArgTypes>(__args)...);
63 #if _LIBCPP_STD_VER > 14
65 reference_wrapper(_Tp&) -> reference_wrapper<_Tp>;
69 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
70 reference_wrapper<_Tp>
71 ref(_Tp& __t) _NOEXCEPT
73 return reference_wrapper<_Tp>(__t);
77 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
78 reference_wrapper<_Tp>
79 ref(reference_wrapper<_Tp> __t) _NOEXCEPT
85 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
86 reference_wrapper<const _Tp>
87 cref(const _Tp& __t) _NOEXCEPT
89 return reference_wrapper<const _Tp>(__t);
93 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
94 reference_wrapper<const _Tp>
95 cref(reference_wrapper<_Tp> __t) _NOEXCEPT
100 template <class _Tp> void ref(const _Tp&&) = delete;
101 template <class _Tp> void cref(const _Tp&&) = delete;
103 _LIBCPP_END_NAMESPACE_STD
105 #endif // _LIBCPP___FUNCTIONAL_REFERENCE_WRAPPER_H