90 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___CXX03___FUNCTIONAL_REFERENCE_WRAPPER_H11#define _LIBCPP___CXX03___FUNCTIONAL_REFERENCE_WRAPPER_H12 13#include <__cxx03/__config>14#include <__cxx03/__functional/weak_result_type.h>15#include <__cxx03/__memory/addressof.h>16#include <__cxx03/__type_traits/enable_if.h>17#include <__cxx03/__type_traits/invoke.h>18#include <__cxx03/__type_traits/is_const.h>19#include <__cxx03/__type_traits/remove_cvref.h>20#include <__cxx03/__type_traits/void_t.h>21#include <__cxx03/__utility/declval.h>22#include <__cxx03/__utility/forward.h>23 24#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)25# pragma GCC system_header26#endif27 28_LIBCPP_BEGIN_NAMESPACE_STD29 30template <class _Tp>31class _LIBCPP_TEMPLATE_VIS reference_wrapper : public __weak_result_type<_Tp> {32public:33 // types34 typedef _Tp type;35 36private:37 type* __f_;38 39 static void __fun(_Tp&) _NOEXCEPT;40 static void __fun(_Tp&&) = delete; // NOLINT(modernize-use-equals-delete) ; This is llvm.org/PR5427641 42public:43 template <class _Up,44 class = __void_t<decltype(__fun(std::declval<_Up>()))>,45 __enable_if_t<!__is_same_uncvref<_Up, reference_wrapper>::value, int> = 0>46 _LIBCPP_HIDE_FROM_ABI reference_wrapper(_Up&& __u) {47 type& __f = static_cast<_Up&&>(__u);48 __f_ = std::addressof(__f);49 }50 51 // access52 _LIBCPP_HIDE_FROM_ABI operator type&() const _NOEXCEPT { return *__f_; }53 _LIBCPP_HIDE_FROM_ABI type& get() const _NOEXCEPT { return *__f_; }54 55 // invoke56 template <class... _ArgTypes>57 _LIBCPP_HIDE_FROM_ABI typename __invoke_of<type&, _ArgTypes...>::type operator()(_ArgTypes&&... __args) const {58 return std::__invoke(get(), std::forward<_ArgTypes>(__args)...);59 }60};61 62template <class _Tp>63inline _LIBCPP_HIDE_FROM_ABI reference_wrapper<_Tp> ref(_Tp& __t) _NOEXCEPT {64 return reference_wrapper<_Tp>(__t);65}66 67template <class _Tp>68inline _LIBCPP_HIDE_FROM_ABI reference_wrapper<_Tp> ref(reference_wrapper<_Tp> __t) _NOEXCEPT {69 return __t;70}71 72template <class _Tp>73inline _LIBCPP_HIDE_FROM_ABI reference_wrapper<const _Tp> cref(const _Tp& __t) _NOEXCEPT {74 return reference_wrapper<const _Tp>(__t);75}76 77template <class _Tp>78inline _LIBCPP_HIDE_FROM_ABI reference_wrapper<const _Tp> cref(reference_wrapper<_Tp> __t) _NOEXCEPT {79 return __t;80}81 82template <class _Tp>83void ref(const _Tp&&) = delete;84template <class _Tp>85void cref(const _Tp&&) = delete;86 87_LIBCPP_END_NAMESPACE_STD88 89#endif // _LIBCPP___CXX03___FUNCTIONAL_REFERENCE_WRAPPER_H90