std::invoke
is a generic way to activate any callable. std::invoke
takes something callable, and arguments to call it with, and does the call. std::invoke( f, args... )
is a slight generalization of typing f(args...)
that also handles a few additional cases.
The advantage of using std::invoke over directly invoking a callable object is that it provides a uniform syntax that works for all callable objects. This means that you can use the same syntax to invoke a function pointer, member function, function object, or lambda, without needing to know the specific type or signature of the callable object.
void foo(int x) {
std::cout << "foo(" << x << ")" << std::endl;
}
struct bar {
void operator()(int x) const {
std::cout << "bar(" << x << ")" << std::endl;
}
};
now you can invoke them as followings:
- Invoke a function pointer:
int x = 42;
std::invoke(foo, x);
- Invoke a member function:
bar b;
std::invoke(&bar::operator(), b, x);
- Invoke a function object:
std::invoke(std::function<void(int)>([](int x) { std::cout << "lambda(" << x << ")" << std::endl; }), x);