reference_wrapper.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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-exception
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef _LIBCPP___FUNCTIONAL_REFERENCE_WRAPPER_H
  10. #define _LIBCPP___FUNCTIONAL_REFERENCE_WRAPPER_H
  11. #include <__config>
  12. #include <__functional/invoke.h>
  13. #include <__functional/weak_result_type.h>
  14. #include <__memory/addressof.h>
  15. #include <__type_traits/enable_if.h>
  16. #include <__type_traits/remove_cvref.h>
  17. #include <__type_traits/void_t.h>
  18. #include <__utility/declval.h>
  19. #include <__utility/forward.h>
  20. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  21. # pragma GCC system_header
  22. #endif
  23. _LIBCPP_BEGIN_NAMESPACE_STD
  24. template <class _Tp>
  25. class _LIBCPP_TEMPLATE_VIS reference_wrapper : public __weak_result_type<_Tp> {
  26. public:
  27. // types
  28. typedef _Tp type;
  29. private:
  30. type* __f_;
  31. static void __fun(_Tp&) _NOEXCEPT;
  32. static void __fun(_Tp&&) = delete;
  33. public:
  34. template <class _Up,
  35. class = __void_t<decltype(__fun(std::declval<_Up>()))>,
  36. __enable_if_t<!__is_same_uncvref<_Up, reference_wrapper>::value, int> = 0>
  37. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reference_wrapper(_Up&& __u)
  38. _NOEXCEPT_(noexcept(__fun(std::declval<_Up>()))) {
  39. type& __f = static_cast<_Up&&>(__u);
  40. __f_ = std::addressof(__f);
  41. }
  42. // access
  43. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 operator type&() const _NOEXCEPT { return *__f_; }
  44. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 type& get() const _NOEXCEPT { return *__f_; }
  45. // invoke
  46. template <class... _ArgTypes>
  47. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 typename __invoke_of<type&, _ArgTypes...>::type
  48. operator()(_ArgTypes&&... __args) const
  49. #if _LIBCPP_STD_VER >= 17
  50. // Since is_nothrow_invocable requires C++17 LWG3764 is not backported
  51. // to earlier versions.
  52. noexcept(is_nothrow_invocable_v<_Tp&, _ArgTypes...>)
  53. #endif
  54. {
  55. return std::__invoke(get(), std::forward<_ArgTypes>(__args)...);
  56. }
  57. };
  58. #if _LIBCPP_STD_VER >= 17
  59. template <class _Tp>
  60. reference_wrapper(_Tp&) -> reference_wrapper<_Tp>;
  61. #endif
  62. template <class _Tp>
  63. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reference_wrapper<_Tp> ref(_Tp& __t) _NOEXCEPT {
  64. return reference_wrapper<_Tp>(__t);
  65. }
  66. template <class _Tp>
  67. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reference_wrapper<_Tp>
  68. ref(reference_wrapper<_Tp> __t) _NOEXCEPT {
  69. return __t;
  70. }
  71. template <class _Tp>
  72. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reference_wrapper<const _Tp> cref(const _Tp& __t) _NOEXCEPT {
  73. return reference_wrapper<const _Tp>(__t);
  74. }
  75. template <class _Tp>
  76. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reference_wrapper<const _Tp>
  77. cref(reference_wrapper<_Tp> __t) _NOEXCEPT {
  78. return __t;
  79. }
  80. template <class _Tp>
  81. void ref(const _Tp&&) = delete;
  82. template <class _Tp>
  83. void cref(const _Tp&&) = delete;
  84. _LIBCPP_END_NAMESPACE_STD
  85. #endif // _LIBCPP___FUNCTIONAL_REFERENCE_WRAPPER_H