xref: /freebsd/contrib/llvm-project/libcxx/include/__functional/perfect_forward.h (revision d0b2dbfa0ecf2bbc9709efc5e20baf8e4b44bbbf)
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... _BoundArgs>
29 struct __perfect_forward_impl;
30 
31 template <class _Op, size_t... _Idx, class... _BoundArgs>
32 struct __perfect_forward_impl<_Op, index_sequence<_Idx...>, _BoundArgs...> {
33 private:
34   tuple<_BoundArgs...> __bound_args_;
35 
36 public:
37   template <class... _Args, class = enable_if_t<
38     is_constructible_v<tuple<_BoundArgs...>, _Args&&...>
39   >>
40   explicit constexpr __perfect_forward_impl(_Args&&... __bound_args)
41     : __bound_args_(_VSTD::forward<_Args>(__bound_args)...) {}
42 
43   __perfect_forward_impl(__perfect_forward_impl const&) = default;
44   __perfect_forward_impl(__perfect_forward_impl&&) = default;
45 
46   __perfect_forward_impl& operator=(__perfect_forward_impl const&) = default;
47   __perfect_forward_impl& operator=(__perfect_forward_impl&&) = default;
48 
49   template <class... _Args, class = enable_if_t<is_invocable_v<_Op, _BoundArgs&..., _Args...>>>
50   _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Args&&... __args) &
51     noexcept(noexcept(_Op()(_VSTD::get<_Idx>(__bound_args_)..., _VSTD::forward<_Args>(__args)...)))
52     -> decltype(      _Op()(_VSTD::get<_Idx>(__bound_args_)..., _VSTD::forward<_Args>(__args)...))
53     { return          _Op()(_VSTD::get<_Idx>(__bound_args_)..., _VSTD::forward<_Args>(__args)...); }
54 
55   template <class... _Args, class = enable_if_t<!is_invocable_v<_Op, _BoundArgs&..., _Args...>>>
56   auto operator()(_Args&&...) & = delete;
57 
58   template <class... _Args, class = enable_if_t<is_invocable_v<_Op, _BoundArgs const&..., _Args...>>>
59   _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Args&&... __args) const&
60     noexcept(noexcept(_Op()(_VSTD::get<_Idx>(__bound_args_)..., _VSTD::forward<_Args>(__args)...)))
61     -> decltype(      _Op()(_VSTD::get<_Idx>(__bound_args_)..., _VSTD::forward<_Args>(__args)...))
62     { return          _Op()(_VSTD::get<_Idx>(__bound_args_)..., _VSTD::forward<_Args>(__args)...); }
63 
64   template <class... _Args, class = enable_if_t<!is_invocable_v<_Op, _BoundArgs const&..., _Args...>>>
65   auto operator()(_Args&&...) const& = delete;
66 
67   template <class... _Args, class = enable_if_t<is_invocable_v<_Op, _BoundArgs..., _Args...>>>
68   _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Args&&... __args) &&
69     noexcept(noexcept(_Op()(_VSTD::get<_Idx>(_VSTD::move(__bound_args_))..., _VSTD::forward<_Args>(__args)...)))
70     -> decltype(      _Op()(_VSTD::get<_Idx>(_VSTD::move(__bound_args_))..., _VSTD::forward<_Args>(__args)...))
71     { return          _Op()(_VSTD::get<_Idx>(_VSTD::move(__bound_args_))..., _VSTD::forward<_Args>(__args)...); }
72 
73   template <class... _Args, class = enable_if_t<!is_invocable_v<_Op, _BoundArgs..., _Args...>>>
74   auto operator()(_Args&&...) && = delete;
75 
76   template <class... _Args, class = enable_if_t<is_invocable_v<_Op, _BoundArgs const..., _Args...>>>
77   _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Args&&... __args) const&&
78     noexcept(noexcept(_Op()(_VSTD::get<_Idx>(_VSTD::move(__bound_args_))..., _VSTD::forward<_Args>(__args)...)))
79     -> decltype(      _Op()(_VSTD::get<_Idx>(_VSTD::move(__bound_args_))..., _VSTD::forward<_Args>(__args)...))
80     { return          _Op()(_VSTD::get<_Idx>(_VSTD::move(__bound_args_))..., _VSTD::forward<_Args>(__args)...); }
81 
82   template <class... _Args, class = enable_if_t<!is_invocable_v<_Op, _BoundArgs const..., _Args...>>>
83   auto operator()(_Args&&...) const&& = delete;
84 };
85 
86 // __perfect_forward implements a perfect-forwarding call wrapper as explained in [func.require].
87 template <class _Op, class ..._Args>
88 using __perfect_forward = __perfect_forward_impl<_Op, index_sequence_for<_Args...>, _Args...>;
89 
90 #endif // _LIBCPP_STD_VER > 14
91 
92 _LIBCPP_END_NAMESPACE_STD
93 
94 #endif // _LIBCPP___FUNCTIONAL_PERFECT_FORWARD_H
95