invoke.h 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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_INVOKE_H
  10. #define _LIBCPP___FUNCTIONAL_INVOKE_H
  11. #include <__config>
  12. #include <__type_traits/invoke.h>
  13. #include <__utility/forward.h>
  14. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  15. # pragma GCC system_header
  16. #endif
  17. _LIBCPP_BEGIN_NAMESPACE_STD
  18. #if _LIBCPP_STD_VER >= 17
  19. template <class _Fn, class ..._Args>
  20. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 invoke_result_t<_Fn, _Args...>
  21. invoke(_Fn&& __f, _Args&&... __args)
  22. noexcept(is_nothrow_invocable_v<_Fn, _Args...>)
  23. {
  24. return _VSTD::__invoke(_VSTD::forward<_Fn>(__f), _VSTD::forward<_Args>(__args)...);
  25. }
  26. #endif // _LIBCPP_STD_VER >= 17
  27. #if _LIBCPP_STD_VER >= 23
  28. template <class _Result, class _Fn, class... _Args>
  29. requires is_invocable_r_v<_Result, _Fn, _Args...>
  30. _LIBCPP_HIDE_FROM_ABI constexpr _Result
  31. invoke_r(_Fn&& __f, _Args&&... __args) noexcept(is_nothrow_invocable_r_v<_Result, _Fn, _Args...>) {
  32. if constexpr (is_void_v<_Result>) {
  33. static_cast<void>(std::invoke(std::forward<_Fn>(__f), std::forward<_Args>(__args)...));
  34. } else {
  35. // TODO: Use reference_converts_from_temporary_v once implemented
  36. // using _ImplicitInvokeResult = invoke_result_t<_Fn, _Args...>;
  37. // static_assert(!reference_converts_from_temporary_v<_Result, _ImplicitInvokeResult>,
  38. static_assert(true,
  39. "Returning from invoke_r would bind a temporary object to the reference return type, "
  40. "which would result in a dangling reference.");
  41. return std::invoke(std::forward<_Fn>(__f), std::forward<_Args>(__args)...);
  42. }
  43. }
  44. #endif
  45. _LIBCPP_END_NAMESPACE_STD
  46. #endif // _LIBCPP___FUNCTIONAL_INVOKE_H