Functors and lambdas
This section will delve into functional objects – their definition, usefulness, and proper usage. We’ll begin by examining an example of a functional object used with an STL algorithm and discuss potential issues, such as the creation of temporary objects and dangling references. After that, we’ll move on to exploring lambda expressions – what they are, how to use them, and specific situations where they can be especially advantageous.
Exploring functional objects
In the Scope, storage duration, and lifetimes section, we looked at various types of object initialization in C++, but our focus was mostly on objects that represent data, such as integers or coordinates. In this section, we’ll shift our attention to another type of object – those designed to be callable, such as a function, but with a crucial difference: they can maintain a state between different function calls. These objects are known as functional objects...