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_BIND_FRONT_H
11#define _LIBCPP___FUNCTIONAL_BIND_FRONT_H
12
13#include <__config>
14#include <__functional/invoke.h>
15#include <__functional/perfect_forward.h>
16#include <__type_traits/conjunction.h>
17#include <__type_traits/decay.h>
18#include <__type_traits/enable_if.h>
19#include <__type_traits/is_constructible.h>
20#include <__type_traits/is_member_pointer.h>
21#include <__type_traits/is_pointer.h>
22#include <__utility/forward.h>
23
24#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
25# pragma GCC system_header
26#endif
27
28_LIBCPP_BEGIN_NAMESPACE_STD
29
30#if _LIBCPP_STD_VER >= 20
31
32struct __bind_front_op {
33 template <class... _Args>
34 _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Args&&... __args) const noexcept(
35 noexcept(std::invoke(std::forward<_Args>(__args)...))) -> decltype(std::invoke(std::forward<_Args>(__args)...)) {
36 return std::invoke(std::forward<_Args>(__args)...);
37 }
38};
39
40template <class _Fn, class... _BoundArgs>
41struct __bind_front_t : __perfect_forward<__bind_front_op, _Fn, _BoundArgs...> {
42 using __perfect_forward<__bind_front_op, _Fn, _BoundArgs...>::__perfect_forward;
43};
44
45template <class _Fn, class... _Args>
46 requires is_constructible_v<decay_t<_Fn>, _Fn> && is_move_constructible_v<decay_t<_Fn>> &&
47 (is_constructible_v<decay_t<_Args>, _Args> && ...) && (is_move_constructible_v<decay_t<_Args>> && ...)
48[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto bind_front(_Fn&& __f, _Args&&... __args) {
49 return __bind_front_t<decay_t<_Fn>, decay_t<_Args>...>(std::forward<_Fn>(__f), std::forward<_Args>(__args)...);
50}
51
52#endif // _LIBCPP_STD_VER >= 20
53
54#if _LIBCPP_STD_VER >= 26
55
56template <auto _Fn, class _Indices, class... _BoundArgs>
57struct __nttp_bind_front_t;
58
59template <auto _Fn, size_t... _Indices, class... _BoundArgs>
60struct __nttp_bind_front_t<_Fn, index_sequence<_Indices...>, _BoundArgs...> {
61 tuple<_BoundArgs...> __bound_args_;
62
63 template <class _Self, class... _Args>
64 _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(this _Self&& __self, _Args&&... __args) noexcept(noexcept(std::invoke(
65 _Fn, std::get<_Indices>(std::forward<_Self>(__self).__bound_args_)..., std::forward<_Args>(__args)...)))
66 -> decltype(std::invoke(
67 _Fn, std::get<_Indices>(std::forward<_Self>(__self).__bound_args_)..., std::forward<_Args>(__args)...)) {
68 return std::invoke(
69 _Fn, std::get<_Indices>(std::forward<_Self>(__self).__bound_args_)..., std::forward<_Args>(__args)...);
70 }
71};
72
73template <auto _Fn>
74struct __nttp_bind_without_bound_args_t {
75 template <class... _Args>
76 _LIBCPP_HIDE_FROM_ABI static constexpr auto
77 operator()(_Args&&... __args) noexcept(noexcept(std::invoke(_Fn, std::forward<_Args>(__args)...)))
78 -> decltype(std::invoke(_Fn, std::forward<_Args>(__args)...)) {
79 return std::invoke(_Fn, std::forward<_Args>(__args)...);
80 }
81};
82
83template <auto _Fn, class... _Args>
84[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto bind_front(_Args&&... __args) {
85 static_assert((is_constructible_v<decay_t<_Args>, _Args> && ...),
86 "bind_front requires all decay_t<Args> to be constructible from respective Args");
87 static_assert((is_move_constructible_v<decay_t<_Args>> && ...),
88 "bind_front requires all decay_t<Args> to be move constructible");
89 if constexpr (using _Ty = decltype(_Fn); is_pointer_v<_Ty> || is_member_pointer_v<_Ty>)
90 static_assert(_Fn != nullptr, "bind_front: f cannot be equal to nullptr");
91
92 if constexpr (sizeof...(_Args) == 0)
93 return __nttp_bind_without_bound_args_t<_Fn>{};
94 else
95 return __nttp_bind_front_t<_Fn, index_sequence_for<_Args...>, decay_t<_Args>...>{
96 .__bound_args_{std::forward<_Args>(__args)...}};
97}
98
99#endif // _LIBCPP_STD_VER >= 26
100
101_LIBCPP_END_NAMESPACE_STD
102
103#endif // _LIBCPP___FUNCTIONAL_BIND_FRONT_H
104