perfect_forward.h 4.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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_PERFECT_FORWARD_H
  10. #define _LIBCPP___FUNCTIONAL_PERFECT_FORWARD_H
  11. #include <__config>
  12. #include <__utility/declval.h>
  13. #include <__utility/forward.h>
  14. #include <__utility/move.h>
  15. #include <tuple>
  16. #include <type_traits>
  17. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  18. # pragma GCC system_header
  19. #endif
  20. _LIBCPP_BEGIN_NAMESPACE_STD
  21. #if _LIBCPP_STD_VER > 14
  22. template <class _Op, class _Indices, class... _BoundArgs>
  23. struct __perfect_forward_impl;
  24. template <class _Op, size_t... _Idx, class... _BoundArgs>
  25. struct __perfect_forward_impl<_Op, index_sequence<_Idx...>, _BoundArgs...> {
  26. private:
  27. tuple<_BoundArgs...> __bound_args_;
  28. public:
  29. template <class... _Args, class = enable_if_t<
  30. is_constructible_v<tuple<_BoundArgs...>, _Args&&...>
  31. >>
  32. explicit constexpr __perfect_forward_impl(_Args&&... __bound_args)
  33. : __bound_args_(_VSTD::forward<_Args>(__bound_args)...) {}
  34. __perfect_forward_impl(__perfect_forward_impl const&) = default;
  35. __perfect_forward_impl(__perfect_forward_impl&&) = default;
  36. __perfect_forward_impl& operator=(__perfect_forward_impl const&) = default;
  37. __perfect_forward_impl& operator=(__perfect_forward_impl&&) = default;
  38. template <class... _Args, class = enable_if_t<is_invocable_v<_Op, _BoundArgs&..., _Args...>>>
  39. _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Args&&... __args) &
  40. noexcept(noexcept(_Op()(_VSTD::get<_Idx>(__bound_args_)..., _VSTD::forward<_Args>(__args)...)))
  41. -> decltype( _Op()(_VSTD::get<_Idx>(__bound_args_)..., _VSTD::forward<_Args>(__args)...))
  42. { return _Op()(_VSTD::get<_Idx>(__bound_args_)..., _VSTD::forward<_Args>(__args)...); }
  43. template <class... _Args, class = enable_if_t<!is_invocable_v<_Op, _BoundArgs&..., _Args...>>>
  44. auto operator()(_Args&&...) & = delete;
  45. template <class... _Args, class = enable_if_t<is_invocable_v<_Op, _BoundArgs const&..., _Args...>>>
  46. _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Args&&... __args) const&
  47. noexcept(noexcept(_Op()(_VSTD::get<_Idx>(__bound_args_)..., _VSTD::forward<_Args>(__args)...)))
  48. -> decltype( _Op()(_VSTD::get<_Idx>(__bound_args_)..., _VSTD::forward<_Args>(__args)...))
  49. { return _Op()(_VSTD::get<_Idx>(__bound_args_)..., _VSTD::forward<_Args>(__args)...); }
  50. template <class... _Args, class = enable_if_t<!is_invocable_v<_Op, _BoundArgs const&..., _Args...>>>
  51. auto operator()(_Args&&...) const& = delete;
  52. template <class... _Args, class = enable_if_t<is_invocable_v<_Op, _BoundArgs..., _Args...>>>
  53. _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Args&&... __args) &&
  54. noexcept(noexcept(_Op()(_VSTD::get<_Idx>(_VSTD::move(__bound_args_))..., _VSTD::forward<_Args>(__args)...)))
  55. -> decltype( _Op()(_VSTD::get<_Idx>(_VSTD::move(__bound_args_))..., _VSTD::forward<_Args>(__args)...))
  56. { return _Op()(_VSTD::get<_Idx>(_VSTD::move(__bound_args_))..., _VSTD::forward<_Args>(__args)...); }
  57. template <class... _Args, class = enable_if_t<!is_invocable_v<_Op, _BoundArgs..., _Args...>>>
  58. auto operator()(_Args&&...) && = delete;
  59. template <class... _Args, class = enable_if_t<is_invocable_v<_Op, _BoundArgs const..., _Args...>>>
  60. _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Args&&... __args) const&&
  61. noexcept(noexcept(_Op()(_VSTD::get<_Idx>(_VSTD::move(__bound_args_))..., _VSTD::forward<_Args>(__args)...)))
  62. -> decltype( _Op()(_VSTD::get<_Idx>(_VSTD::move(__bound_args_))..., _VSTD::forward<_Args>(__args)...))
  63. { return _Op()(_VSTD::get<_Idx>(_VSTD::move(__bound_args_))..., _VSTD::forward<_Args>(__args)...); }
  64. template <class... _Args, class = enable_if_t<!is_invocable_v<_Op, _BoundArgs const..., _Args...>>>
  65. auto operator()(_Args&&...) const&& = delete;
  66. };
  67. // __perfect_forward implements a perfect-forwarding call wrapper as explained in [func.require].
  68. template <class _Op, class ..._Args>
  69. using __perfect_forward = __perfect_forward_impl<_Op, index_sequence_for<_Args...>, _Args...>;
  70. #endif // _LIBCPP_STD_VER > 14
  71. _LIBCPP_END_NAMESPACE_STD
  72. #endif // _LIBCPP___FUNCTIONAL_PERFECT_FORWARD_H