bind_front.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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_FRONT_H
  10. #define _LIBCPP___FUNCTIONAL_BIND_FRONT_H
  11. #include <__config>
  12. #include <__functional/invoke.h>
  13. #include <__functional/perfect_forward.h>
  14. #include <__type_traits/conjunction.h>
  15. #include <__type_traits/decay.h>
  16. #include <__type_traits/enable_if.h>
  17. #include <__type_traits/is_constructible.h>
  18. #include <__utility/forward.h>
  19. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  20. # pragma GCC system_header
  21. #endif
  22. _LIBCPP_BEGIN_NAMESPACE_STD
  23. #if _LIBCPP_STD_VER >= 20
  24. struct __bind_front_op {
  25. template <class... _Args>
  26. _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Args&&... __args) const
  27. noexcept(noexcept(std::invoke(std::forward<_Args>(__args)...)))
  28. -> decltype(std::invoke(std::forward<_Args>(__args)...)) {
  29. return std::invoke(std::forward<_Args>(__args)...);
  30. }
  31. };
  32. template <class _Fn, class... _BoundArgs>
  33. struct __bind_front_t : __perfect_forward<__bind_front_op, _Fn, _BoundArgs...> {
  34. using __perfect_forward<__bind_front_op, _Fn, _BoundArgs...>::__perfect_forward;
  35. };
  36. template <class _Fn, class... _Args>
  37. requires is_constructible_v<decay_t<_Fn>, _Fn> && is_move_constructible_v<decay_t<_Fn>> &&
  38. (is_constructible_v<decay_t<_Args>, _Args> && ...) && (is_move_constructible_v<decay_t<_Args>> && ...)
  39. _LIBCPP_HIDE_FROM_ABI constexpr auto bind_front(_Fn&& __f, _Args&&... __args) {
  40. return __bind_front_t<decay_t<_Fn>, decay_t<_Args>...>(std::forward<_Fn>(__f), std::forward<_Args>(__args)...);
  41. }
  42. #endif // _LIBCPP_STD_VER >= 20
  43. _LIBCPP_END_NAMESPACE_STD
  44. #endif // _LIBCPP___FUNCTIONAL_BIND_FRONT_H