bind_back.h 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 _Bound, class ..._Args>
  28. _LIBCPP_HIDE_FROM_ABI
  29. constexpr auto operator()(_Fn&& __f, _Bound&& __bound, _Args&& ...__args) const
  30. noexcept(noexcept(_VSTD::invoke(_VSTD::forward<_Fn>(__f), _VSTD::forward<_Args>(__args)..., _VSTD::get<_Ip>(_VSTD::forward<_Bound>(__bound))...)))
  31. -> decltype( _VSTD::invoke(_VSTD::forward<_Fn>(__f), _VSTD::forward<_Args>(__args)..., _VSTD::get<_Ip>(_VSTD::forward<_Bound>(__bound))...))
  32. { return _VSTD::invoke(_VSTD::forward<_Fn>(__f), _VSTD::forward<_Args>(__args)..., _VSTD::get<_Ip>(_VSTD::forward<_Bound>(__bound))...); }
  33. };
  34. template <class _Fn, class _BoundArgs>
  35. struct __bind_back_t : __perfect_forward<__bind_back_op<tuple_size_v<_BoundArgs>>, _Fn, _BoundArgs> {
  36. using __perfect_forward<__bind_back_op<tuple_size_v<_BoundArgs>>, _Fn, _BoundArgs>::__perfect_forward;
  37. };
  38. template <class _Fn, class ..._Args, class = enable_if_t<
  39. _And<
  40. is_constructible<decay_t<_Fn>, _Fn>,
  41. is_move_constructible<decay_t<_Fn>>,
  42. is_constructible<decay_t<_Args>, _Args>...,
  43. is_move_constructible<decay_t<_Args>>...
  44. >::value
  45. >>
  46. _LIBCPP_HIDE_FROM_ABI
  47. constexpr auto __bind_back(_Fn&& __f, _Args&&... __args)
  48. noexcept(noexcept(__bind_back_t<decay_t<_Fn>, tuple<decay_t<_Args>...>>(_VSTD::forward<_Fn>(__f), _VSTD::forward_as_tuple(_VSTD::forward<_Args>(__args)...))))
  49. -> decltype( __bind_back_t<decay_t<_Fn>, tuple<decay_t<_Args>...>>(_VSTD::forward<_Fn>(__f), _VSTD::forward_as_tuple(_VSTD::forward<_Args>(__args)...)))
  50. { return __bind_back_t<decay_t<_Fn>, tuple<decay_t<_Args>...>>(_VSTD::forward<_Fn>(__f), _VSTD::forward_as_tuple(_VSTD::forward<_Args>(__args)...)); }
  51. #endif // _LIBCPP_STD_VER > 17
  52. _LIBCPP_END_NAMESPACE_STD
  53. #endif // _LIBCPP___FUNCTIONAL_BIND_BACK_H