bind_back.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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_BIND_BACK_H
  10. #define _LIBCPP___FUNCTIONAL_BIND_BACK_H
  11. #include <__config>
  12. #include <__functional/invoke.h>
  13. #include <__functional/perfect_forward.h>
  14. #include <__utility/forward.h>
  15. #include <__utility/integer_sequence.h>
  16. #include <tuple>
  17. #include <type_traits>
  18. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  19. # pragma GCC system_header
  20. #endif
  21. _LIBCPP_BEGIN_NAMESPACE_STD
  22. #if _LIBCPP_STD_VER > 17
  23. template <size_t _NBound, class = make_index_sequence<_NBound>>
  24. struct __bind_back_op;
  25. template <size_t _NBound, size_t ..._Ip>
  26. struct __bind_back_op<_NBound, index_sequence<_Ip...>> {
  27. template <class _Fn, class _BoundArgs, class... _Args>
  28. _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Fn&& __f, _BoundArgs&& __bound_args, _Args&&... __args) const
  29. noexcept(noexcept(_VSTD::invoke(_VSTD::forward<_Fn>(__f), _VSTD::forward<_Args>(__args)..., _VSTD::get<_Ip>(_VSTD::forward<_BoundArgs>(__bound_args))...)))
  30. -> decltype( _VSTD::invoke(_VSTD::forward<_Fn>(__f), _VSTD::forward<_Args>(__args)..., _VSTD::get<_Ip>(_VSTD::forward<_BoundArgs>(__bound_args))...))
  31. { return _VSTD::invoke(_VSTD::forward<_Fn>(__f), _VSTD::forward<_Args>(__args)..., _VSTD::get<_Ip>(_VSTD::forward<_BoundArgs>(__bound_args))...); }
  32. };
  33. template <class _Fn, class _BoundArgs>
  34. struct __bind_back_t : __perfect_forward<__bind_back_op<tuple_size_v<_BoundArgs>>, _Fn, _BoundArgs> {
  35. using __perfect_forward<__bind_back_op<tuple_size_v<_BoundArgs>>, _Fn, _BoundArgs>::__perfect_forward;
  36. };
  37. template <class _Fn, class ..._Args, class = enable_if_t<
  38. _And<
  39. is_constructible<decay_t<_Fn>, _Fn>,
  40. is_move_constructible<decay_t<_Fn>>,
  41. is_constructible<decay_t<_Args>, _Args>...,
  42. is_move_constructible<decay_t<_Args>>...
  43. >::value
  44. >>
  45. _LIBCPP_HIDE_FROM_ABI
  46. constexpr auto __bind_back(_Fn&& __f, _Args&&... __args)
  47. noexcept(noexcept(__bind_back_t<decay_t<_Fn>, tuple<decay_t<_Args>...>>(_VSTD::forward<_Fn>(__f), _VSTD::forward_as_tuple(_VSTD::forward<_Args>(__args)...))))
  48. -> decltype( __bind_back_t<decay_t<_Fn>, tuple<decay_t<_Args>...>>(_VSTD::forward<_Fn>(__f), _VSTD::forward_as_tuple(_VSTD::forward<_Args>(__args)...)))
  49. { return __bind_back_t<decay_t<_Fn>, tuple<decay_t<_Args>...>>(_VSTD::forward<_Fn>(__f), _VSTD::forward_as_tuple(_VSTD::forward<_Args>(__args)...)); }
  50. #endif // _LIBCPP_STD_VER > 17
  51. _LIBCPP_END_NAMESPACE_STD
  52. #endif // _LIBCPP___FUNCTIONAL_BIND_BACK_H