C++C++17C++20Learn C++Templates

How To Use std::invoke In C++ 17?

How To Use stdinvoke In C++ 17

There is a new library feature in the C++17 standard, it is std::invoke which is a useful feature to uniformly invoke callable entities. In this post, we explain what std::invoke is and how we can use it in examples. First, let’s remind ourselves about what is a callable object and what is a functor in modern C++.

What is callable object and what is functor in modern C++?

callable object (some call it a functor) is an object that can be used as a function or function pointer by using the operator(). This term is not the same as a function term in programming. We can pass many arguments with them; thus, we don’t need to define many global variables, we can use these kinds of variables in the scope that we use.

Here you can find more details about it.

What is std::invoke in C++ 17?

The std::invoke call is a library feature in C++ 17 that allows invoking a method at run time and improved in C++20 and C++23 with invoke_r. It is defined in the <functional> header and useful to write libraries with the same behavior as the standard’s magic INVOKE rule. You can use std::invoke to call a function or method, a lambda expression, or a member function, or can be used to access a data member, or you can use to invoke a function object.

In C++17 it is defined as below,

In C++20 it is defined as below,

And since C++23, there is invoke_r and it is defined as below,

How can we use std::invoke with a parametric function in C++17?

Here is a simple std::invoke example with a parametric function.

How can we use std::invoke with a lambda in C++17?

Here is a simple std::invoke example with a lambda.

How can we use std::invoke with a function object in C++17?

Here is a simple std::invoke example with a function object.

How can we use std::invoke with a struct in C++17?

Here is a simple std::invoke example with a struct that invokes a member function.

How can we use std::invoke with a method of an object in C++17?

Here is a simple std::invoke example with a class object that invokes a method of that object.

As you see the std::invoke is used to support the caller of a code part that is passing any callable object such as function, method, function object, struct, lambda, etc. Note that C++17 brings std::is_invocablestd::is_invocable_rstd::is_nothrow_invocablestd::is_nothrow_invocable_r that traits to reason about invocability and invocation results.

In addition to std::invoke, in C++ Builder and Delphi programming languages there is System::Rtti::Invoke method for the RTTI (Run Time Type Information) Objects. Invoke methods are useful when the actual method is not known at compile time but is rather looked up at run time.

For more details about this feature in C++17 standard, please see these papers; N4169

Learn C++ Optimization With A Genetic Algorithms Example

C++ Builder is the easiest and fastest C and C++ compiler and IDE for building simple or professional applications on the Windows operating system. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for UIs.

There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here.

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial

Free C++Builder Community Edition

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17C++20Learn C++Syntax

What Is Set (std::set) In Modern C++?

C++C++11C++14C++17C++20IteratorsLearn C++

What Is Deque (std::deque) In Modern C++?

C++C++17C++20IteratorsLearn C++

What Is forward_list (std::forward_list) In Modern C++?

C++C++17ComponentsGame DevelopmentLanguage FeatureLearn C++

Learn To Create A Torus As A Custom 3D Mesh Object in C++ Builder