The following code example is taken from the book
C++23 - The Complete Guide
by Nicolai M. Josuttis,
Leanpub, 2026
The code is licensed under a
Creative Commons Attribution 4.0 International License.
// raw code
#include <iostream>
#include <functional>
int cube(int i) {
return i * i * i;
}
void call(std::function<int(int)> f) {
if (f) {
std::cout << f(7) << '\n';
}
}
int main()
{
std::function<int(int)> func;
call(func);
func = cube; // refer to ordinary function
call(func);
func = std::negate<int>{}; // refer to function object
call(func);
func = [] (int i) { return i * i; }; // refer to lambda
call(func);
}