xref: /llvm-project/libcxx/include/__functional/invoke.h (revision 09e3a360581dc36d0820d3fb6da9bd7cfed87b5d)
1050b064fSChristopher Di Bella // -*- C++ -*-
2050b064fSChristopher Di Bella //===----------------------------------------------------------------------===//
3050b064fSChristopher Di Bella //
4050b064fSChristopher Di Bella // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5050b064fSChristopher Di Bella // See https://llvm.org/LICENSE.txt for license information.
6050b064fSChristopher Di Bella // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7050b064fSChristopher Di Bella //
8050b064fSChristopher Di Bella //===----------------------------------------------------------------------===//
9050b064fSChristopher Di Bella 
10050b064fSChristopher Di Bella #ifndef _LIBCPP___FUNCTIONAL_INVOKE_H
11050b064fSChristopher Di Bella #define _LIBCPP___FUNCTIONAL_INVOKE_H
12050b064fSChristopher Di Bella 
13050b064fSChristopher Di Bella #include <__config>
14bea2ff65SMark de Wever #include <__type_traits/invoke.h>
15*09e3a360SLouis Dionne #include <__type_traits/is_void.h>
16050b064fSChristopher Di Bella #include <__utility/forward.h>
17050b064fSChristopher Di Bella 
18050b064fSChristopher Di Bella #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
19050b064fSChristopher Di Bella #  pragma GCC system_header
20050b064fSChristopher Di Bella #endif
21050b064fSChristopher Di Bella 
22050b064fSChristopher Di Bella _LIBCPP_BEGIN_NAMESPACE_STD
23050b064fSChristopher Di Bella 
244f15267dSNikolas Klauser #if _LIBCPP_STD_VER >= 17
25050b064fSChristopher Di Bella 
26050b064fSChristopher Di Bella template <class _Fn, class... _Args>
275146b57bSNikolas Klauser _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 invoke_result_t<_Fn, _Args...>
289783f28cSLouis Dionne invoke(_Fn&& __f, _Args&&... __args) noexcept(is_nothrow_invocable_v<_Fn, _Args...>) {
2977a00c0dSLouis Dionne   return std::__invoke(std::forward<_Fn>(__f), std::forward<_Args>(__args)...);
30050b064fSChristopher Di Bella }
31050b064fSChristopher Di Bella 
324f15267dSNikolas Klauser #endif // _LIBCPP_STD_VER >= 17
33050b064fSChristopher Di Bella 
3487cc95a9SLouis Dionne #if _LIBCPP_STD_VER >= 23
3587cc95a9SLouis Dionne template <class _Result, class _Fn, class... _Args>
3687cc95a9SLouis Dionne   requires is_invocable_r_v<_Result, _Fn, _Args...>
3787cc95a9SLouis Dionne _LIBCPP_HIDE_FROM_ABI constexpr _Result
3887cc95a9SLouis Dionne invoke_r(_Fn&& __f, _Args&&... __args) noexcept(is_nothrow_invocable_r_v<_Result, _Fn, _Args...>) {
3987cc95a9SLouis Dionne   if constexpr (is_void_v<_Result>) {
4087cc95a9SLouis Dionne     static_cast<void>(std::invoke(std::forward<_Fn>(__f), std::forward<_Args>(__args)...));
4187cc95a9SLouis Dionne   } else {
4287cc95a9SLouis Dionne     // TODO: Use reference_converts_from_temporary_v once implemented
4387cc95a9SLouis Dionne     // using _ImplicitInvokeResult = invoke_result_t<_Fn, _Args...>;
4487cc95a9SLouis Dionne     // static_assert(!reference_converts_from_temporary_v<_Result, _ImplicitInvokeResult>,
4587cc95a9SLouis Dionne     static_assert(true,
4687cc95a9SLouis Dionne                   "Returning from invoke_r would bind a temporary object to the reference return type, "
4787cc95a9SLouis Dionne                   "which would result in a dangling reference.");
4887cc95a9SLouis Dionne     return std::invoke(std::forward<_Fn>(__f), std::forward<_Args>(__args)...);
4987cc95a9SLouis Dionne   }
5087cc95a9SLouis Dionne }
5187cc95a9SLouis Dionne #endif
5287cc95a9SLouis Dionne 
53050b064fSChristopher Di Bella _LIBCPP_END_NAMESPACE_STD
54050b064fSChristopher Di Bella 
55050b064fSChristopher Di Bella #endif // _LIBCPP___FUNCTIONAL_INVOKE_H
56