Please note that all the code examples for this chapter are available as a part of this chapter’s code download on the book’s website at www.wrox.com/go/proc++3e on the Download Code tab.
This chapter discusses some additional library functionality that is available in the C++ standard library and that does not immediately fit into other chapters.
std::function, defined in the
<functional> header file, can be used to create a type that can point to a function, a function object, or a lambda expression; basically anything that is callable. It is called a polymorphic function wrapper. It can be used as a function pointer or as a parameter for a function to implement callbacks. The template parameters for the
std::function template look a bit different than most template parameters. Its syntax is as follows:
R is the return value type of the function and
ArgTypes is a comma-separated list of argument types for the function.
The following example demonstrates how to use
std::function to implement a function pointer. It creates a function pointer
f1 to point to the function
f1 is defined, you can call
func() by using the name