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