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