libstdc++
invoke.h
Go to the documentation of this file.
1// Implementation of INVOKE -*- C++ -*-
2
3// Copyright (C) 2016-2025 Free Software Foundation, Inc.
4//
5// This file is part of the GNU ISO C++ Library. This library is free
6// software; you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 3, or (at your option)
9// any later version.
10
11// This library is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// Under Section 7 of GPL version 3, you are granted additional
17// permissions described in the GCC Runtime Library Exception, version
18// 3.1, as published by the Free Software Foundation.
19
20// You should have received a copy of the GNU General Public License and
21// a copy of the GCC Runtime Library Exception along with this program;
22// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23// <http://www.gnu.org/licenses/>.
24
25/** @file include/bits/invoke.h
26 * This is an internal header file, included by other library headers.
27 * Do not attempt to use it directly. @headername{functional}
28 */
29
30#ifndef _GLIBCXX_INVOKE_H
31#define _GLIBCXX_INVOKE_H 1
32
33#ifdef _GLIBCXX_SYSHDR
34#pragma GCC system_header
35#endif
36
37#if __cplusplus < 201103L
38# include <bits/c++0x_warning.h>
39#else
40
41#include <type_traits>
42#include <bits/move.h> // forward
43
44namespace std _GLIBCXX_VISIBILITY(default)
45{
46_GLIBCXX_BEGIN_NAMESPACE_VERSION
47
48 /**
49 * @addtogroup utilities
50 * @{
51 */
52
53 // Used by __invoke_impl instead of std::forward<_Tp> so that a
54 // reference_wrapper is converted to an lvalue-reference.
55 template<typename _Tp, typename _Up = typename __inv_unwrap<_Tp>::type>
56 constexpr _Up&&
57 __invfwd(typename remove_reference<_Tp>::type& __t) noexcept
58 { return static_cast<_Up&&>(__t); }
59
60 template<typename _Res, typename _Fn, typename... _Args>
61 constexpr _Res
62 __invoke_impl(__invoke_other, _Fn&& __f, _Args&&... __args)
63 { return std::forward<_Fn>(__f)(std::forward<_Args>(__args)...); }
64
65 template<typename _Res, typename _MemFun, typename _Tp, typename... _Args>
66 constexpr _Res
67 __invoke_impl(__invoke_memfun_ref, _MemFun&& __f, _Tp&& __t,
68 _Args&&... __args)
69 { return (__invfwd<_Tp>(__t).*__f)(std::forward<_Args>(__args)...); }
70
71 template<typename _Res, typename _MemFun, typename _Tp, typename... _Args>
72 constexpr _Res
73 __invoke_impl(__invoke_memfun_deref, _MemFun&& __f, _Tp&& __t,
74 _Args&&... __args)
75 {
76 return ((*std::forward<_Tp>(__t)).*__f)(std::forward<_Args>(__args)...);
77 }
78
79 template<typename _Res, typename _MemPtr, typename _Tp>
80 constexpr _Res
81 __invoke_impl(__invoke_memobj_ref, _MemPtr&& __f, _Tp&& __t)
82 { return __invfwd<_Tp>(__t).*__f; }
83
84 template<typename _Res, typename _MemPtr, typename _Tp>
85 constexpr _Res
86 __invoke_impl(__invoke_memobj_deref, _MemPtr&& __f, _Tp&& __t)
87 { return (*std::forward<_Tp>(__t)).*__f; }
88
89 /// Invoke a callable object.
90 template<typename _Callable, typename... _Args>
91 constexpr typename __invoke_result<_Callable, _Args...>::type
92 __invoke(_Callable&& __fn, _Args&&... __args)
93 noexcept(__is_nothrow_invocable<_Callable, _Args...>::value)
94 {
95 using __result = __invoke_result<_Callable, _Args...>;
96 using __type = typename __result::type;
97 using __tag = typename __result::__invoke_type;
98 return std::__invoke_impl<__type>(__tag{}, std::forward<_Callable>(__fn),
99 std::forward<_Args>(__args)...);
100 }
101
102#if __cplusplus >= 201703L
103 // INVOKE<R>: Invoke a callable object and convert the result to R.
104 template<typename _Res, typename _Callable, typename... _Args>
105 constexpr enable_if_t<is_invocable_r_v<_Res, _Callable, _Args...>, _Res>
106 __invoke_r(_Callable&& __fn, _Args&&... __args)
107 noexcept(is_nothrow_invocable_r_v<_Res, _Callable, _Args...>)
108 {
109 using __result = __invoke_result<_Callable, _Args...>;
110 using __type = typename __result::type;
111 using __tag = typename __result::__invoke_type;
112 if constexpr (is_void_v<_Res>)
113 std::__invoke_impl<__type>(__tag{}, std::forward<_Callable>(__fn),
114 std::forward<_Args>(__args)...);
115 else
116 return std::__invoke_impl<__type>(__tag{},
118 std::forward<_Args>(__args)...);
119 }
120#else // C++11 or C++14
121 // This is a non-SFINAE-friendly std::invoke_r<R>(fn, args...) for C++11/14.
122 // It's used in std::function, std::bind, and std::packaged_task. Only
123 // std::function is constrained on is_invocable_r, but that is checked on
124 // construction so doesn't need to be checked again when calling __invoke_r.
125 // Consequently, these __invoke_r overloads do not check for invocable
126 // arguments, nor check that the invoke result is convertible to R.
127
128 // INVOKE<R>: Invoke a callable object and convert the result to R.
129 template<typename _Res, typename _Callable, typename... _Args>
130 constexpr __enable_if_t<!is_void<_Res>::value, _Res>
131 __invoke_r(_Callable&& __fn, _Args&&... __args)
132 {
133 using __result = __invoke_result<_Callable, _Args...>;
134 using __type = typename __result::type;
135#if __has_builtin(__reference_converts_from_temporary)
136 static_assert(!__reference_converts_from_temporary(_Res, __type),
137 "INVOKE<R> must not create a dangling reference");
138#endif
139 using __tag = typename __result::__invoke_type;
140 return std::__invoke_impl<__type>(__tag{}, std::forward<_Callable>(__fn),
141 std::forward<_Args>(__args)...);
142 }
143
144 // INVOKE<R> when R is cv void
145 template<typename _Res, typename _Callable, typename... _Args>
146 _GLIBCXX14_CONSTEXPR __enable_if_t<is_void<_Res>::value, _Res>
147 __invoke_r(_Callable&& __fn, _Args&&... __args)
148 {
149 using __result = __invoke_result<_Callable, _Args...>;
150 using __type = typename __result::type;
151 using __tag = typename __result::__invoke_type;
152 std::__invoke_impl<__type>(__tag{}, std::forward<_Callable>(__fn),
153 std::forward<_Args>(__args)...);
154 }
155#endif // C++11 or C++14
156
157_GLIBCXX_END_NAMESPACE_VERSION
158} // namespace std
159
160#endif // C++11
161
162#endif // _GLIBCXX_INVOKE_H
typename enable_if< _Cond, _Tp >::type enable_if_t
Alias template for enable_if.
Definition type_traits:2896
constexpr __invoke_result< _Callable, _Args... >::type __invoke(_Callable &&__fn, _Args &&... __args) noexcept(__is_nothrow_invocable< _Callable, _Args... >::value)
Invoke a callable object.
Definition invoke.h:92
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
Definition move.h:72
ISO C++ entities toplevel namespace is std.