Chapter 19Additional Library Utilities
- How you can use std::function for function pointers
- How to work with compile-time rational numbers
- How to work with time
- How to generate random numbers
- What tuples are and how to use them
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
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:
std::function<R(ArgTypes...)>
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 func(). Once f1 is defined, you can call func() by using the name func or f1 ...