std::function in C++ is a template class used to store and invoke callable objects such as functions, lambda expressions, and functors. It provides a flexible and generic way to handle callable objects in C++ programs.
- Can wrap different types of callable objects in a single interface
- Commonly used in callbacks, event handling, and functional programming
- Improves code flexibility, reusability, and maintainability
Note: std::function is defined in the <functional> header file.
#include <iostream>
#include <functional>
using namespace std;
int add(int a, int b) {
return a + b;
}
int main() {
function<int(int, int)> f = add;
cout << f(10, 20);
return 0;
}
Output
30
Explanation: Above program uses std::function to store the add() function inside a callable wrapper object f. The wrapped function is then invoked using f(10, 20) to calculate and print the sum of two numbers.
Declaration
To create a wrapper object, we first declare it using the following syntax:
std::function< rtype (atype...)> name();
where,
- name: Name of the wrapper.
- atype: Types of the arguments that function takes.
- rtype: Return type of the function you want to store.
Initialization
The above syntax only creates an empty instance of std::function. To wrap a particular function inside this wrapper object, we use assignment operator as shown:
std::function< ret_t (args_t)> name = f;
where f is the function to be wrapped. We can also initialize it at the time of declaration:
std::function< ret_t (args_t)> name(f);
Example: Program to illustrate the working of std::function
#include <bits/stdc++.h>
using namespace std;
int f(int a, int b) {
return a + b;
}
int main() {
// std::function wrapping traditional
// function
function<int(int, int)> calc = f;
cout << "Sum: " << calc(8, 2) << endl;
// std::function wrapping a lambda
// expression
calc = [](int a, int b) { return a * b; };
cout << "Product: " << calc(8, 2);
return 0;
}
Output
Sum: 10 Product: 16
Member Functions of std::function
std::function contains some member functions to provide some basic functionality. The following table lists some useful member functions of std::function class template:
Function | Description |
|---|---|
swap() | Swaps the wrapped callable of two std::function objects. |
operator bool | Checks if the |
operator () | Invoke the callable with the given arguments. |
target() | Returns a pointer to the stored callable. If there is no callable stored, returns nullptr. |
target_type() | Returns the |
Applications of std::functions in C++
Apart from the applications shown in the above examples, std::function can also be used for:
- Callbacks: Used for event-driven systems where functions are passed and called later when an event occurs.
- Function Wrapping and Higher-Order Functions: Allows passing functions as parameters and returning them.
- Stateful Callbacks: Enables callbacks with preserved states, making it easier to manage state without global variables.
- Replacing Function Pointers: Provides a safer and more flexible alternative to traditional function pointers, supporting multiple callable types.
Examples of std::function
The following examples demonstrates the usage of std::function in C++ for different applications such as callbacks, state preserved callbacks, function composition.
Example 1: Passing std::function as Argument (Callback)
#include <bits/stdc++.h>
using namespace std;
// Functions for simple math operations
int add(int a, int b) {
return a + b;
}
int sub(int a, int b) {
return a - b;
}
int mul(int a, int b) {
return a * b;
}
int divs(int a, int b) {
return a / b;
}
// Using std::function as parameter
void func(int a, int b,
function<int(int, int)> calc) {
int res = calc(a, b);
cout << "Result: " << res << endl;
}
int main() {
// Calling all the arithmetic functions
func(8, 2, add);
func(8, 2, sub);
func(8, 2, mul);
func(8, 2, divs);
return 0;
}
Output
Result: 10 Result: 6 Result: 16 Result: 4
Example 2: Wrapping Member Functions of a Class
We can also wrap a member function of a class using std::function object but we have to add one extra argument as shown in the below program.
#include <bits/stdc++.h>
using namespace std;
class C {
public:
int f(int a, int b) {
return a * b;
}
};
int main() {
C c;
// Wrapping member function of C
function<int(C&, int, int)> calc = &C::f;
// Call the member function using function
if (calc)
cout << "Product: " << calc(c, 4, 5);
else
cout << "No Callable Assigned";
return 0;
}
Output
Product: 20
Example 3: Composing Two Functions into One
#include <bits/stdc++.h>
using namespace std;
// Composed function
function<int(int)> cf(function<int(int)> f1,
function<int(int)> f2) {
// Returning a lambda expression that
// in turn returns a function
return [f1, f2](int x) {
// Apply f1 first, then f2
return f2(f1(x));
};
}
int main() {
auto add = [](int x) { return x + 2; };
auto mul = [](int x) { return x * 3; };
function<int(int)> calc = cf(add, mul);
cout << calc(4);
return 0;
}
Output
18
Explanation: In this example, we composted two lambda functions add() and mul() into one function cf() which takes two function wrappers and returns another function wrapper. Then we created a wrapper for this using this composed function and use it to perform the addition and multiplication operator at one call.