C++ Reference ยท Memory and Utilities
C++ std::invoke function
Reference for std::invoke and std::invoke_r: call functions, lambdas, member functions and data members uniformly, plus is_invocable and invoke_result.
std::invoke (header <functional>, C++17) calls any callable with a given list of arguments using one uniform syntax. It works for ordinary functions, function pointers, lambdas, function objects, pointers to member functions and even pointers to data members. Generic code that receives "something callable" from a caller should use std::invoke instead of writing f(args...), because f(args...) does not work for member pointers.
For a pointer to a member, the first argument is the object: a reference, a pointer, or a smart pointer. std::invoke_r (C++23) additionally converts the result to a type you choose.
struct Dog { std::string name() const { return "Rex"; } }; Dog d; auto s = std::invoke(&Dog::name, d); // "Rex"
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Syntax
template<class F, class... Args> std::invoke_result_t<F, Args...> invoke(F&& f, Args&&... args) noexcept(std::is_nothrow_invocable_v<F, Args...>); // C++17 template<class R, class F, class... Args> constexpr R invoke_r(F&& f, Args&&... args) noexcept(/* ... */); // C++23
| Name | Description |
|---|---|
f |
The callable, or a pointer to a data or function member. |
args |
The arguments. For a member pointer the first one is the object (reference, pointer, reference_wrapper or smart pointer). |
R |
For invoke_r: the type the result is implicitly converted to. void discards the result. |
Returns: whatever the callable returns, with the same value category (a reference stays a reference).
Errors / undefined behavior: if the call is ill-formed, the program does not compile. Calling a member function on a null pointer is undefined behavior as usual. Exceptions thrown by the callable propagate.
Usage notes
- Why not
f(args...)? A pointer to member is not callable with the function-call operator.std::invokehandles both cases, so library code such asstd::function, thread constructors andstd::applyuse it internally. - Object forms for member pointers:
std::invoke(&C::m, obj),std::invoke(&C::m, &obj),std::invoke(&C::m, std::ref(obj))andstd::invoke(&C::m, smartPtr)all work. Anything dereferenceable with*is accepted. - Data members are "called" with only the object:
std::invoke(&C::field, obj)returns a reference to the field, which preserves constness of the object. - Traits:
std::is_invocable_v<F, Args...>,std::is_nothrow_invocable_vandstd::invoke_result_t(all in<type_traits>) let you constrain templates, see<type_traits>. std::applyandstd::invoke:std::apply(f, tuple)callsfwith the elements of a tuple using the same rules.- Ranges projections use the same mechanism:
std::ranges::sort(v, {}, &Person::age)sorts by member throughstd::invoke. - Cost: none beyond the underlying call; the compiler inlines it in normal builds.
- Version notes:
invokeis C++17,invoke_ris C++23,is_invocableandinvoke_resultare C++17 too.