C++ program to demonstrate how to pass functions as parameters using std::function. Here's an explanation and breakdown of the code:
-
Function Pointers: In C++, functions can be passed as parameters to other functions using pointers. However,
std::functionis a more flexible way to handle callable objects (functions, lambdas, etc.). -
std::function: This is a wrapper around callable objects (like functions, lambdas, or functors) and provides more versatility. You can use it to pass different kinds of callable entities as parameters to other functions.
#include <functional>
#include <iostream>
using namespace std;
// Define add and multiply to return respective values
int add(int x, int y) { return x + y; }
int multiply(int x, int y) { return x * y; }
Here, two functions add and multiply are defined to add and multiply two integers, respectively.
// Function that accepts an object of type std::function<> as a parameter
int invoke(int x, int y, function<int(int, int)> func)
{
return func(x, y);
}
- The
invokefunction takes three parameters: two integers x and y, and astd::functionobjectfuncthat expects two integers as input and returns an integer. - Inside the function,
funcis invoked with x and y as arguments, and the result is returned. This function allows you to pass any callable object (function, lambda, etc.) that matches the signatureint(int, int).
int main()
{
// Pass the required function as a parameter using its name
cout << "Addition of 20 and 10 is ";
cout << invoke(20, 10, &add) << '\n';
cout << "Multiplication of 20 and 10 is ";
cout << invoke(20, 10, &multiply) << '\n';
return 0;
}
In the main function:
- The invoke function is called twice. The first call passes
&add, a pointer to the add function, and the second call passes&multiply, a pointer to the multiply function. - As
invokeis called, it executes the function (addormultiply) with the given arguments (20 and 10), and prints the result. Output:
Addition of 20 and 10 is 30
Multiplication of 20 and 10 is 200
- Flexibility: The
std::functiontype is highly versatile and can store pointers to functions, lambdas, or even objects that overload the operator(). - Polymorphism for Callable Objects: You can change the behavior of the invoke function by passing different types of functions or callables, which enhances modularity. This approach provides a clean way to manage and invoke functions dynamically, making it useful for scenarios where functions need to be passed around as first-class citizens.