C++ General-Purpose Polymorphic Function Wrapper
Introduction
C++ class template std::function
is a general-purpose polymorphic function wrapper. It could wrap functions, functors, and lambda expressions allowing them to use the same argument type in the higher-order function.
In this blog post, I would like to show how to use std::function
.
Examples
1 |
|
To compile the program, please run the following command in the terminal.
1 | $ g++ std_function.cpp -o std_function -std=c++11 |
Conclusions
std::function
is highly polymorphic, general-purpose, and versatile. So there will be significant runtime overhead. However, because it is so general-purpose, it is often used in the C++ callback interface so that the user could pass different kinds of callable implementations.
C++ General-Purpose Polymorphic Function Wrapper