Chapter 2. Writing User-Defined Classes with Modern C++ Features
User-defined classes have been a mainstay in financial C++ development from the beginning, because instruments and data (such as bonds, options contracts, and rate curves) can be naturally represented by objects. One useful feature that has existed prior to C++11 is the overloading of the parentheses ()
operator, which enables an object to be utilized as a function object, or functor. In general, functors are objects that “behave like functions, but, like objects, can manage additional data.”1 As you will see, this is particularly convenient when we might need to find the root of a function, such as when calculating the implied volatility of a traded options contract.
A newer form of a functor, called a lambda expression (also known as a lambda function or just a lambda), was added to the language with C++11, as previously discussed in Chapter 1. This chapter covers lambdas in further detail, with examples of how they can be used for refactoring functionality into a single location and thus reduce code duplication. Lambdas can also be passed as function arguments. Functors, including lambda expressions, will also be key within the context of Standard Template Library (STL) algorithms, covered in Chapter 5.
Move semantics were another major addition to the C++11 Standard, allowing an often significant efficiency gain by transferring possession of a constructor input argument to its respective member variable. This ...
Get Learning Modern C++ for Finance now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.