compose.h 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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_COMPOSE_H
  10. #define _LIBCPP___FUNCTIONAL_COMPOSE_H
  11. #include <__config>
  12. #include <__functional/invoke.h>
  13. #include <__functional/perfect_forward.h>
  14. #include <__type_traits/decay.h>
  15. #include <__utility/forward.h>
  16. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  17. # pragma GCC system_header
  18. #endif
  19. _LIBCPP_BEGIN_NAMESPACE_STD
  20. #if _LIBCPP_STD_VER >= 20
  21. struct __compose_op {
  22. template <class _Fn1, class _Fn2, class... _Args>
  23. _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Fn1&& __f1, _Fn2&& __f2, _Args&&... __args) const noexcept(noexcept(
  24. std::invoke(std::forward<_Fn1>(__f1), std::invoke(std::forward<_Fn2>(__f2), std::forward<_Args>(__args)...))))
  25. -> decltype(std::invoke(std::forward<_Fn1>(__f1),
  26. std::invoke(std::forward<_Fn2>(__f2), std::forward<_Args>(__args)...))) {
  27. return std::invoke(std::forward<_Fn1>(__f1), std::invoke(std::forward<_Fn2>(__f2), std::forward<_Args>(__args)...));
  28. }
  29. };
  30. template <class _Fn1, class _Fn2>
  31. struct __compose_t : __perfect_forward<__compose_op, _Fn1, _Fn2> {
  32. using __perfect_forward<__compose_op, _Fn1, _Fn2>::__perfect_forward;
  33. };
  34. template <class _Fn1, class _Fn2>
  35. _LIBCPP_HIDE_FROM_ABI constexpr auto __compose(_Fn1&& __f1, _Fn2&& __f2) noexcept(
  36. noexcept(__compose_t<decay_t<_Fn1>, decay_t<_Fn2>>(std::forward<_Fn1>(__f1), std::forward<_Fn2>(__f2))))
  37. -> decltype(__compose_t<decay_t<_Fn1>, decay_t<_Fn2>>(std::forward<_Fn1>(__f1), std::forward<_Fn2>(__f2))) {
  38. return __compose_t<decay_t<_Fn1>, decay_t<_Fn2>>(std::forward<_Fn1>(__f1), std::forward<_Fn2>(__f2));
  39. }
  40. #endif // _LIBCPP_STD_VER >= 20
  41. _LIBCPP_END_NAMESPACE_STD
  42. #endif // _LIBCPP___FUNCTIONAL_COMPOSE_H