C++17: std::invoke & std::apply

std::invoke

std::invoke is used for calling function objects, like lambdas, methods, functions without knowing about type this function.

For example , before we needed to use pointer to function, pointer to method:

with std::invoke it’s much more simpler:

std::invoke it’s universal interface for calling anything it’s callable.

Example with lambda:

Example with template:

Here is standard document: n4169.

std::apply

std::apply is used to Invoke a Callable object with a tuple of arguments.

For example:

 

std::make_from_tuple – Construct an object of type T, using the elements of the tuple t as the arguments to the constructor.

Due to guaranteed copy elision, T need not be movable.

For example:

 

Leave a Reply

Your email address will not be published. Required fields are marked *