| Defined in header <functional>
|
||
template< class F, class... Args > std::invoke_result_t<F, Args...> invoke( F&& f, Args&&... args ) noexcept(/* see below */); |
(1) | (since C++17) (constexpr since C++20) |
template< class R, class F, class... Args > constexpr R invoke_r( F&& f, Args&&... args ) noexcept(/* see below */); |
(2) | (since C++23) |
1) Invoke the Callable object
f with the parameters args as by INVOKE(std::forward<F>(f), std::forward<Args>(args)...). This overload participates in overload resolution only if std::is_invocable_v<F, Args...> is true.2) Invoke the Callable object
f with the parameters args as by INVOKE<R>(std::forward<F>(f), std::forward<Args>(args)...). This overload participates in overload resolution only if std::is_invocable_r_v<R, F, Args...> is true.Parameters
| f | - | Callable object to be invoked |
| args | - | arguments to pass to f
|
Return value
1) The value returned by
f.2) The value returned by
f, implicitly converted to R, if R is not (possibly cv-qualified) void. None otherwise.Exceptions
1)
noexcept specification:
noexcept(std::is_nothrow_invocable_v<F, Args...>)
2)
noexcept specification:
noexcept(std::is_nothrow_invocable_r_v<R, F, Args...>)
Possible implementation
| invoke (1) |
|---|
|
| invoke_r (2) |
|
Notes
| Feature-test macro | Value | Std | Feature |
|---|---|---|---|
__cpp_lib_invoke |
201411L |
(C++17) | std::invoke, (1)
|
__cpp_lib_invoke_r |
202106L |
(C++23) | std::invoke_r, (2)
|
Example
#include <functional>
#include <iostream>
#include <type_traits>
struct Foo
{
Foo(int num) : num_(num) {}
void print_add(int i) const { std::cout << num_ + i << '\n'; }
int num_;
};
void print_num(int i)
{
std::cout << i << '\n';
}
struct PrintNum
{
void operator()(int i) const
{
std::cout << i << '\n';
}
};
int main()
{
std::cout << "invoke a free function: ";
std::invoke(print_num, -9);
std::cout << "invoke a lambda: ";
std::invoke([](){ print_num(42); });
std::cout << "invoke a member function: ";
const Foo foo(314159);
std::invoke(&Foo::print_add, foo, 1);
std::cout << "invoke (i.e., access) a data member num_: "
<< std::invoke(&Foo::num_, foo) << '\n';
std::cout << "invoke a function object: ";
std::invoke(PrintNum(), 18);
#if defined(__cpp_lib_invoke_r)
auto add = [](int x, int y){ return x + y; };
std::cout << "invoke a lambda converting result to float: ";
auto ret = std::invoke_r<float>(add, 11, 22);
static_assert(std::is_same<decltype(ret), float>());
std::cout << std::fixed << ret << "\ninvoke print_num: ";
std::invoke_r<void>(print_num, 44);
#endif
}invoke a free function: -9
invoke a lambda: 42
invoke a member function: 314160
invoke (i.e., access) a data member num_: 314159
invoke a function object: 18
invoke a lambda converting result to float: 33.000000
invoke print_num: 44See also
| (C++11) |
creates a function object out of a pointer to a member (function template) |
| (C++11)(removed in C++20)(C++17) |
deduces the result type of invoking a callable object with a set of arguments (class template) |
| checks if a type can be invoked (as if by std::invoke) with the given argument types (class template) | |
| (C++17) |
calls a function with a tuple of arguments (function template) |
Content from this page on cppreference.com, licensed under CC-BY-SA 3.0 and GFDL. Modified: restyled, site navigation removed, code windows added. Offline snapshot of 9 February 2025.