Path: blob/main/contrib/llvm-project/libcxx/include/__functional/reference_wrapper.h
35259 views
// -*- C++ -*-1//===----------------------------------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//89#ifndef _LIBCPP___FUNCTIONAL_REFERENCE_WRAPPER_H10#define _LIBCPP___FUNCTIONAL_REFERENCE_WRAPPER_H1112#include <__compare/synth_three_way.h>13#include <__concepts/boolean_testable.h>14#include <__config>15#include <__functional/invoke.h>16#include <__functional/weak_result_type.h>17#include <__memory/addressof.h>18#include <__type_traits/enable_if.h>19#include <__type_traits/is_const.h>20#include <__type_traits/remove_cvref.h>21#include <__type_traits/void_t.h>22#include <__utility/declval.h>23#include <__utility/forward.h>2425#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)26# pragma GCC system_header27#endif2829_LIBCPP_BEGIN_NAMESPACE_STD3031template <class _Tp>32class _LIBCPP_TEMPLATE_VIS reference_wrapper : public __weak_result_type<_Tp> {33public:34// types35typedef _Tp type;3637private:38type* __f_;3940static void __fun(_Tp&) _NOEXCEPT;41static void __fun(_Tp&&) = delete; // NOLINT(modernize-use-equals-delete) ; This is llvm.org/PR542764243public:44template <class _Up,45class = __void_t<decltype(__fun(std::declval<_Up>()))>,46__enable_if_t<!__is_same_uncvref<_Up, reference_wrapper>::value, int> = 0>47_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reference_wrapper(_Up&& __u)48_NOEXCEPT_(noexcept(__fun(std::declval<_Up>()))) {49type& __f = static_cast<_Up&&>(__u);50__f_ = std::addressof(__f);51}5253// access54_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 operator type&() const _NOEXCEPT { return *__f_; }55_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 type& get() const _NOEXCEPT { return *__f_; }5657// invoke58template <class... _ArgTypes>59_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 typename __invoke_of<type&, _ArgTypes...>::type60operator()(_ArgTypes&&... __args) const61#if _LIBCPP_STD_VER >= 1762// Since is_nothrow_invocable requires C++17 LWG3764 is not backported63// to earlier versions.64noexcept(is_nothrow_invocable_v<_Tp&, _ArgTypes...>)65#endif66{67return std::__invoke(get(), std::forward<_ArgTypes>(__args)...);68}6970#if _LIBCPP_STD_VER >= 267172// [refwrap.comparisons], comparisons7374_LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(reference_wrapper __x, reference_wrapper __y)75requires requires {76{ __x.get() == __y.get() } -> __boolean_testable;77}78{79return __x.get() == __y.get();80}8182_LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(reference_wrapper __x, const _Tp& __y)83requires requires {84{ __x.get() == __y } -> __boolean_testable;85}86{87return __x.get() == __y;88}8990_LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(reference_wrapper __x, reference_wrapper<const _Tp> __y)91requires(!is_const_v<_Tp>) && requires {92{ __x.get() == __y.get() } -> __boolean_testable;93}94{95return __x.get() == __y.get();96}9798_LIBCPP_HIDE_FROM_ABI friend constexpr auto operator<=>(reference_wrapper __x, reference_wrapper __y)99requires requires { std::__synth_three_way(__x.get(), __y.get()); }100{101return std::__synth_three_way(__x.get(), __y.get());102}103104_LIBCPP_HIDE_FROM_ABI friend constexpr auto operator<=>(reference_wrapper __x, const _Tp& __y)105requires requires { std::__synth_three_way(__x.get(), __y); }106{107return std::__synth_three_way(__x.get(), __y);108}109110_LIBCPP_HIDE_FROM_ABI friend constexpr auto operator<=>(reference_wrapper __x, reference_wrapper<const _Tp> __y)111requires(!is_const_v<_Tp>) && requires { std::__synth_three_way(__x.get(), __y.get()); }112{113return std::__synth_three_way(__x.get(), __y.get());114}115116#endif // _LIBCPP_STD_VER >= 26117};118119#if _LIBCPP_STD_VER >= 17120template <class _Tp>121reference_wrapper(_Tp&) -> reference_wrapper<_Tp>;122#endif123124template <class _Tp>125inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reference_wrapper<_Tp> ref(_Tp& __t) _NOEXCEPT {126return reference_wrapper<_Tp>(__t);127}128129template <class _Tp>130inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reference_wrapper<_Tp>131ref(reference_wrapper<_Tp> __t) _NOEXCEPT {132return __t;133}134135template <class _Tp>136inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reference_wrapper<const _Tp> cref(const _Tp& __t) _NOEXCEPT {137return reference_wrapper<const _Tp>(__t);138}139140template <class _Tp>141inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reference_wrapper<const _Tp>142cref(reference_wrapper<_Tp> __t) _NOEXCEPT {143return __t;144}145146template <class _Tp>147void ref(const _Tp&&) = delete;148template <class _Tp>149void cref(const _Tp&&) = delete;150151_LIBCPP_END_NAMESPACE_STD152153#endif // _LIBCPP___FUNCTIONAL_REFERENCE_WRAPPER_H154155156