O'Reilly logo

Financial Instrument Pricing Using C++, 2nd Edition by Daniel J. Duffy

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

CHAPTER 6Type Traits, Advanced Lambdas and Multiparadigm Design in C++

6.1 Introduction and Objectives

In this chapter we introduce a number of advanced concepts and language features in C++. First, we discuss the C++ type traits library. For example, we can provide answers to the following kinds of questions:

  • Is a type a floating-point type?
  • Is a type an arithmetic type?
  • Does a type have a defined constructor?
  • Are two types the same?
  • Remove const and volatile specifiers from a given type.
  • Remove a reference/pointer from a given type.
  • Make an integral type signed or unsigned.
  • And many more.

These advanced topics will be useful for library builders. Application developers can browse the topics here although they should become familiar with Sections 6.5, 6.6.1 and 6.8 on a first reading.

A simple example is to determine whether a type is arithmetic, in other words whether it supports the usual arithmetic operators for addition, subtraction, multiplication and division. For example, we may need to exclude non-arithmetic types in a generic matrix algebra library. A typical code snippet is:

#include <iostream>
#include <complex>
#include <string>
#include <ratio>
#include <type-traits>
 
 
 
// #include <boost/type_traits.hpp> // Not needed anymore, // now in C++   template <typename T> void IsArithmetic() { // First example of type_traits to check if T supports +,−,*,/   if (std::is_arithmetic<T>::value) { std::cout << "This is an arithmetic type argument\n"; } else { std::cout ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required