1fe6060f1SDimitry Andric // -*- C++ -*- 2fe6060f1SDimitry Andric //===----------------------------------------------------------------------===// 3fe6060f1SDimitry Andric // 4fe6060f1SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5fe6060f1SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 6fe6060f1SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7fe6060f1SDimitry Andric // 8fe6060f1SDimitry Andric //===----------------------------------------------------------------------===// 9fe6060f1SDimitry Andric 10fe6060f1SDimitry Andric #ifndef _LIBCPP___FUNCTIONAL_INVOKE_H 11fe6060f1SDimitry Andric #define _LIBCPP___FUNCTIONAL_INVOKE_H 12fe6060f1SDimitry Andric 13fe6060f1SDimitry Andric #include <__config> 1406c3fb27SDimitry Andric #include <__type_traits/invoke.h> 15fe6060f1SDimitry Andric #include <__utility/forward.h> 16fe6060f1SDimitry Andric 17fe6060f1SDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 18fe6060f1SDimitry Andric # pragma GCC system_header 19fe6060f1SDimitry Andric #endif 20fe6060f1SDimitry Andric 21fe6060f1SDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD 22fe6060f1SDimitry Andric 2306c3fb27SDimitry Andric #if _LIBCPP_STD_VER >= 17 2481ad6265SDimitry Andric 25fe6060f1SDimitry Andric template <class _Fn, class ..._Args> 26bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 invoke_result_t<_Fn, _Args...> 27fe6060f1SDimitry Andric invoke(_Fn&& __f, _Args&&... __args) 28fe6060f1SDimitry Andric noexcept(is_nothrow_invocable_v<_Fn, _Args...>) 29fe6060f1SDimitry Andric { 30*5f757f3fSDimitry Andric return std::__invoke(std::forward<_Fn>(__f), std::forward<_Args>(__args)...); 31fe6060f1SDimitry Andric } 32fe6060f1SDimitry Andric 3306c3fb27SDimitry Andric #endif // _LIBCPP_STD_VER >= 17 3406c3fb27SDimitry Andric 3506c3fb27SDimitry Andric #if _LIBCPP_STD_VER >= 23 3606c3fb27SDimitry Andric template <class _Result, class _Fn, class... _Args> 3706c3fb27SDimitry Andric requires is_invocable_r_v<_Result, _Fn, _Args...> 3806c3fb27SDimitry Andric _LIBCPP_HIDE_FROM_ABI constexpr _Result 3906c3fb27SDimitry Andric invoke_r(_Fn&& __f, _Args&&... __args) noexcept(is_nothrow_invocable_r_v<_Result, _Fn, _Args...>) { 4006c3fb27SDimitry Andric if constexpr (is_void_v<_Result>) { 4106c3fb27SDimitry Andric static_cast<void>(std::invoke(std::forward<_Fn>(__f), std::forward<_Args>(__args)...)); 4206c3fb27SDimitry Andric } else { 4306c3fb27SDimitry Andric // TODO: Use reference_converts_from_temporary_v once implemented 4406c3fb27SDimitry Andric // using _ImplicitInvokeResult = invoke_result_t<_Fn, _Args...>; 4506c3fb27SDimitry Andric // static_assert(!reference_converts_from_temporary_v<_Result, _ImplicitInvokeResult>, 4606c3fb27SDimitry Andric static_assert(true, 4706c3fb27SDimitry Andric "Returning from invoke_r would bind a temporary object to the reference return type, " 4806c3fb27SDimitry Andric "which would result in a dangling reference."); 4906c3fb27SDimitry Andric return std::invoke(std::forward<_Fn>(__f), std::forward<_Args>(__args)...); 5006c3fb27SDimitry Andric } 5106c3fb27SDimitry Andric } 5206c3fb27SDimitry Andric #endif 53fe6060f1SDimitry Andric 54fe6060f1SDimitry Andric _LIBCPP_END_NAMESPACE_STD 55fe6060f1SDimitry Andric 56fe6060f1SDimitry Andric #endif // _LIBCPP___FUNCTIONAL_INVOKE_H 57