Skip to Main Content
Professional C++, 3rd Edition
book

Professional C++, 3rd Edition

by Marc Gregoire
September 2014
Intermediate to advanced content levelIntermediate to advanced
984 pages
25h 22m
English
Wrox
Content preview from Professional C++, 3rd Edition

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 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Professional C++, Second Edition

Professional C++, Second Edition

Marc Gregoire, Nicholas A. Solter, Scott J. Kleper
Expert C++ - Second Edition

Expert C++ - Second Edition

Marcelo Guerra Hahn, Araks Tigranyan, John Asatryan, Vardan Grigoryan, Shunguang Wu

Publisher Resources

ISBN: 9781118858134Purchase book