Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is std::invoke in c++? [closed]

Tags:

c++

c++17

I've just been reading about std::thread and std::bind which I've faced with the Callable concept and std::invoke.

I read about std::invoke on cppreference but I didn't understand what it said. Here is my question:
What is std::invoke, std::function, std::bind and the Callable concept? And what is relationship between them?

like image 820
user3397145 Avatar asked Apr 28 '17 12:04

user3397145


People also ask

What is std:: invoke for?

The std::invoke function in the C++ standard library is usually used to call a functor with parameters.

What is invoked in c++?

When a function is invoked, you pass a value to the parameter. This value is referred to as actual parameter or argument. The parameter list refers to the type, order, and number of the parameters of a function. Parameters are optional; that is, a function may contain no parameters.


1 Answers

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.

Something callable includes a function pointer or reference, a member function pointer, an object with an operator(), or a pointer to member data.

In the member cases, the first argument is interpreted as the this. Then remaining arguments are passed to () (except in the pointer-to-member-data-case), with std::reference_wrappers unwrapping.

INVOKE was a concept in the C++ standard; C++17 simply exposed a std::invoke which does it directly. I suspect it was exposed partly because it is useful when doing other metaprogramming, partly because every standard library has an implementation of INVOKE in it already and exposing it was basically free, and partly because it makes talking about INVOKE easier when it is a concrete thing.

like image 56
Yakk - Adam Nevraumont Avatar answered Oct 07 '22 22:10

Yakk - Adam Nevraumont