File indexing completed on 2026-05-03 08:13:51
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #ifndef _LIBCPP___FUNCTIONAL_BIND_H
0011 #define _LIBCPP___FUNCTIONAL_BIND_H
0012
0013 #include <__config>
0014 #include <__functional/weak_result_type.h>
0015 #include <__fwd/functional.h>
0016 #include <__type_traits/decay.h>
0017 #include <__type_traits/invoke.h>
0018 #include <__type_traits/is_reference_wrapper.h>
0019 #include <__type_traits/is_void.h>
0020 #include <tuple>
0021
0022 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0023 # pragma GCC system_header
0024 #endif
0025
0026 _LIBCPP_BEGIN_NAMESPACE_STD
0027
0028 template <class _Tp>
0029 struct is_bind_expression
0030 : _If< _IsSame<_Tp, __remove_cvref_t<_Tp> >::value, false_type, is_bind_expression<__remove_cvref_t<_Tp> > > {};
0031
0032 #if _LIBCPP_STD_VER >= 17
0033 template <class _Tp>
0034 inline constexpr bool is_bind_expression_v = is_bind_expression<_Tp>::value;
0035 #endif
0036
0037 template <class _Tp>
0038 struct is_placeholder
0039 : _If< _IsSame<_Tp, __remove_cvref_t<_Tp> >::value,
0040 integral_constant<int, 0>,
0041 is_placeholder<__remove_cvref_t<_Tp> > > {};
0042
0043 #if _LIBCPP_STD_VER >= 17
0044 template <class _Tp>
0045 inline constexpr int is_placeholder_v = is_placeholder<_Tp>::value;
0046 #endif
0047
0048 namespace placeholders {
0049
0050 template <int _Np>
0051 struct __ph {};
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061 _LIBCPP_EXPORTED_FROM_ABI extern const __ph<1> _1;
0062 _LIBCPP_EXPORTED_FROM_ABI extern const __ph<2> _2;
0063 _LIBCPP_EXPORTED_FROM_ABI extern const __ph<3> _3;
0064 _LIBCPP_EXPORTED_FROM_ABI extern const __ph<4> _4;
0065 _LIBCPP_EXPORTED_FROM_ABI extern const __ph<5> _5;
0066 _LIBCPP_EXPORTED_FROM_ABI extern const __ph<6> _6;
0067 _LIBCPP_EXPORTED_FROM_ABI extern const __ph<7> _7;
0068 _LIBCPP_EXPORTED_FROM_ABI extern const __ph<8> _8;
0069 _LIBCPP_EXPORTED_FROM_ABI extern const __ph<9> _9;
0070 _LIBCPP_EXPORTED_FROM_ABI extern const __ph<10> _10;
0071
0072 }
0073
0074 template <int _Np>
0075 struct is_placeholder<placeholders::__ph<_Np> > : public integral_constant<int, _Np> {};
0076
0077 #ifndef _LIBCPP_CXX03_LANG
0078
0079 template <class _Tp, class _Uj>
0080 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp& __mu(reference_wrapper<_Tp> __t, _Uj&) {
0081 return __t.get();
0082 }
0083
0084 template <class _Ti, class... _Uj, size_t... _Indx>
0085 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __invoke_result_t<_Ti&, _Uj...>
0086 __mu_expand(_Ti& __ti, tuple<_Uj...>& __uj, __tuple_indices<_Indx...>) {
0087 return __ti(std::forward<_Uj>(std::get<_Indx>(__uj))...);
0088 }
0089
0090 template <class _Ti, class... _Uj, __enable_if_t<is_bind_expression<_Ti>::value, int> = 0>
0091 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __invoke_result_t<_Ti&, _Uj...>
0092 __mu(_Ti& __ti, tuple<_Uj...>& __uj) {
0093 typedef typename __make_tuple_indices<sizeof...(_Uj)>::type __indices;
0094 return std::__mu_expand(__ti, __uj, __indices());
0095 }
0096
0097 template <bool _IsPh, class _Ti, class _Uj>
0098 struct __mu_return2 {};
0099
0100 template <class _Ti, class _Uj>
0101 struct __mu_return2<true, _Ti, _Uj> {
0102 typedef typename tuple_element<is_placeholder<_Ti>::value - 1, _Uj>::type type;
0103 };
0104
0105 template <class _Ti, class _Uj, __enable_if_t<0 < is_placeholder<_Ti>::value, int> = 0>
0106 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
0107 typename __mu_return2<0 < is_placeholder<_Ti>::value, _Ti, _Uj>::type
0108 __mu(_Ti&, _Uj& __uj) {
0109 const size_t __indx = is_placeholder<_Ti>::value - 1;
0110 return std::forward<typename tuple_element<__indx, _Uj>::type>(std::get<__indx>(__uj));
0111 }
0112
0113 template <class _Ti,
0114 class _Uj,
0115 __enable_if_t<!is_bind_expression<_Ti>::value && is_placeholder<_Ti>::value == 0 &&
0116 !__is_reference_wrapper<_Ti>::value,
0117 int> = 0>
0118 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Ti& __mu(_Ti& __ti, _Uj&) {
0119 return __ti;
0120 }
0121
0122 template <class _Ti, bool _IsReferenceWrapper, bool _IsBindEx, bool _IsPh, class _TupleUj>
0123 struct __mu_return_impl;
0124
0125 template <bool _Invokable, class _Ti, class... _Uj>
0126 struct __mu_return_invokable
0127 {
0128 typedef __nat type;
0129 };
0130
0131 template <class _Ti, class... _Uj>
0132 struct __mu_return_invokable<true, _Ti, _Uj...> {
0133 using type = __invoke_result_t<_Ti&, _Uj...>;
0134 };
0135
0136 template <class _Ti, class... _Uj>
0137 struct __mu_return_impl<_Ti, false, true, false, tuple<_Uj...> >
0138 : public __mu_return_invokable<__is_invocable_v<_Ti&, _Uj...>, _Ti, _Uj...> {};
0139
0140 template <class _Ti, class _TupleUj>
0141 struct __mu_return_impl<_Ti, false, false, true, _TupleUj> {
0142 typedef typename tuple_element<is_placeholder<_Ti>::value - 1, _TupleUj>::type&& type;
0143 };
0144
0145 template <class _Ti, class _TupleUj>
0146 struct __mu_return_impl<_Ti, true, false, false, _TupleUj> {
0147 typedef typename _Ti::type& type;
0148 };
0149
0150 template <class _Ti, class _TupleUj>
0151 struct __mu_return_impl<_Ti, false, false, false, _TupleUj> {
0152 typedef _Ti& type;
0153 };
0154
0155 template <class _Ti, class _TupleUj>
0156 struct __mu_return
0157 : public __mu_return_impl<
0158 _Ti,
0159 __is_reference_wrapper<_Ti>::value,
0160 is_bind_expression<_Ti>::value,
0161 0 < is_placeholder<_Ti>::value && is_placeholder<_Ti>::value <= tuple_size<_TupleUj>::value,
0162 _TupleUj> {};
0163
0164 template <class _Fp, class _BoundArgs, class _TupleUj>
0165 struct __is_valid_bind_return {
0166 static const bool value = false;
0167 };
0168
0169 template <class _Fp, class... _BoundArgs, class _TupleUj>
0170 struct __is_valid_bind_return<_Fp, tuple<_BoundArgs...>, _TupleUj> {
0171 static const bool value = __is_invocable_v<_Fp, typename __mu_return<_BoundArgs, _TupleUj>::type...>;
0172 };
0173
0174 template <class _Fp, class... _BoundArgs, class _TupleUj>
0175 struct __is_valid_bind_return<_Fp, const tuple<_BoundArgs...>, _TupleUj> {
0176 static const bool value = __is_invocable_v<_Fp, typename __mu_return<const _BoundArgs, _TupleUj>::type...>;
0177 };
0178
0179 template <class _Fp, class _BoundArgs, class _TupleUj, bool = __is_valid_bind_return<_Fp, _BoundArgs, _TupleUj>::value>
0180 struct __bind_return;
0181
0182 template <class _Fp, class... _BoundArgs, class _TupleUj>
0183 struct __bind_return<_Fp, tuple<_BoundArgs...>, _TupleUj, true> {
0184 using type = __invoke_result_t< _Fp&, typename __mu_return< _BoundArgs, _TupleUj >::type... >;
0185 };
0186
0187 template <class _Fp, class... _BoundArgs, class _TupleUj>
0188 struct __bind_return<_Fp, const tuple<_BoundArgs...>, _TupleUj, true> {
0189 using type = __invoke_result_t< _Fp&, typename __mu_return< const _BoundArgs, _TupleUj >::type... >;
0190 };
0191
0192 template <class _Fp, class _BoundArgs, size_t... _Indx, class _Args>
0193 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 typename __bind_return<_Fp, _BoundArgs, _Args>::type
0194 __apply_functor(_Fp& __f, _BoundArgs& __bound_args, __tuple_indices<_Indx...>, _Args&& __args) {
0195 return std::__invoke(__f, std::__mu(std::get<_Indx>(__bound_args), __args)...);
0196 }
0197
0198 template <class _Fp, class... _BoundArgs>
0199 class __bind : public __weak_result_type<__decay_t<_Fp> > {
0200 protected:
0201 using _Fd _LIBCPP_NODEBUG = __decay_t<_Fp>;
0202 typedef tuple<__decay_t<_BoundArgs>...> _Td;
0203
0204 private:
0205 _Fd __f_;
0206 _Td __bound_args_;
0207
0208 typedef typename __make_tuple_indices<sizeof...(_BoundArgs)>::type __indices;
0209
0210 public:
0211 template <
0212 class _Gp,
0213 class... _BA,
0214 __enable_if_t<is_constructible<_Fd, _Gp>::value && !is_same<__libcpp_remove_reference_t<_Gp>, __bind>::value,
0215 int> = 0>
0216 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit __bind(_Gp&& __f, _BA&&... __bound_args)
0217 : __f_(std::forward<_Gp>(__f)), __bound_args_(std::forward<_BA>(__bound_args)...) {}
0218
0219 template <class... _Args>
0220 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 typename __bind_return<_Fd, _Td, tuple<_Args&&...> >::type
0221 operator()(_Args&&... __args) {
0222 return std::__apply_functor(__f_, __bound_args_, __indices(), tuple<_Args&&...>(std::forward<_Args>(__args)...));
0223 }
0224
0225 template <class... _Args>
0226 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
0227 typename __bind_return<const _Fd, const _Td, tuple<_Args&&...> >::type
0228 operator()(_Args&&... __args) const {
0229 return std::__apply_functor(__f_, __bound_args_, __indices(), tuple<_Args&&...>(std::forward<_Args>(__args)...));
0230 }
0231 };
0232
0233 template <class _Fp, class... _BoundArgs>
0234 struct is_bind_expression<__bind<_Fp, _BoundArgs...> > : public true_type {};
0235
0236 template <class _Rp, class _Fp, class... _BoundArgs>
0237 class __bind_r : public __bind<_Fp, _BoundArgs...> {
0238 typedef __bind<_Fp, _BoundArgs...> base;
0239 typedef typename base::_Fd _Fd;
0240 typedef typename base::_Td _Td;
0241
0242 public:
0243 typedef _Rp result_type;
0244
0245 template <
0246 class _Gp,
0247 class... _BA,
0248 __enable_if_t<is_constructible<_Fd, _Gp>::value && !is_same<__libcpp_remove_reference_t<_Gp>, __bind_r>::value,
0249 int> = 0>
0250 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit __bind_r(_Gp&& __f, _BA&&... __bound_args)
0251 : base(std::forward<_Gp>(__f), std::forward<_BA>(__bound_args)...) {}
0252
0253 template <
0254 class... _Args,
0255 __enable_if_t<is_convertible<typename __bind_return<_Fd, _Td, tuple<_Args&&...> >::type, result_type>::value ||
0256 is_void<_Rp>::value,
0257 int> = 0>
0258 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 result_type operator()(_Args&&... __args) {
0259 return std::__invoke_r<_Rp>(static_cast<base&>(*this), std::forward<_Args>(__args)...);
0260 }
0261
0262 template <class... _Args,
0263 __enable_if_t<is_convertible<typename __bind_return<const _Fd, const _Td, tuple<_Args&&...> >::type,
0264 result_type>::value ||
0265 is_void<_Rp>::value,
0266 int> = 0>
0267 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 result_type operator()(_Args&&... __args) const {
0268 return std::__invoke_r<_Rp>(static_cast<base const&>(*this), std::forward<_Args>(__args)...);
0269 }
0270 };
0271
0272 template <class _Rp, class _Fp, class... _BoundArgs>
0273 struct is_bind_expression<__bind_r<_Rp, _Fp, _BoundArgs...> > : public true_type {};
0274
0275 template <class _Fp, class... _BoundArgs>
0276 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __bind<_Fp, _BoundArgs...>
0277 bind(_Fp&& __f, _BoundArgs&&... __bound_args) {
0278 typedef __bind<_Fp, _BoundArgs...> type;
0279 return type(std::forward<_Fp>(__f), std::forward<_BoundArgs>(__bound_args)...);
0280 }
0281
0282 template <class _Rp, class _Fp, class... _BoundArgs>
0283 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __bind_r<_Rp, _Fp, _BoundArgs...>
0284 bind(_Fp&& __f, _BoundArgs&&... __bound_args) {
0285 typedef __bind_r<_Rp, _Fp, _BoundArgs...> type;
0286 return type(std::forward<_Fp>(__f), std::forward<_BoundArgs>(__bound_args)...);
0287 }
0288
0289 #endif
0290
0291 _LIBCPP_END_NAMESPACE_STD
0292
0293 #endif