Reader small image

You're reading from  C++ High Performance - Second Edition

Product typeBook
Published inDec 2020
Reading LevelIntermediate
PublisherPackt
ISBN-139781839216541
Edition2nd Edition
Languages
Right arrow
Authors (2):
Björn Andrist
Björn Andrist
author image
Björn Andrist

Björn Andrist is a freelance software consultant currently focusing on audio applications. For more than 15 years, he has been working professionally with C++ in projects ranging from UNIX server applications to real-time audio applications on desktop and mobile. In the past, he has also taught courses in algorithms and data structures, concurrent programming, and programming methodologies. Björn holds a BS in computer engineering and an MS in computer science from KTH Royal Institute of Technology.
Read more about Björn Andrist

Viktor Sehr
Viktor Sehr
author image
Viktor Sehr

Viktor Sehr is the founder and main developer of the small game studio Toppluva AB. At Toppluva he develops a custom graphics engine which powers the open-world skiing game Grand Mountain Adventure. He has 13 years of professional experience using C++, with real-time graphics, audio, and architectural design as his focus areas. Through his career, he has developed medical visualization software at Mentice and Raysearch Laboratories as well as real-time audio applications at Propellerhead Software. Viktor holds an M.S. in media science from Linköping University.
Read more about Viktor Sehr

View More author details
Right arrow

Essential Utilities

This chapter will introduce some essential classes from the C++ Utility library. Some of the metaprogramming techniques presented in the previous chapter will be used in order to work effectively with collections that contain elements of different types.

C++ containers are homogenous, meaning that they can only store elements of one single type. A std::vector<int> stores a collection of integers and all objects stored in a std::list<Boat> are of type Boat. But sometimes, we need to keep track of a collection of elements of different types. I will refer to these collections as heterogenous collections. In a heterogeneous collection, the elements may have different types. The following figure shows an example of a homogenous collection of ints and a heterogenous collection with elements of different types:

Figure 9.1: Homogenous and heterogenous collections

This chapter will cover a set of useful templates from the C++ Utility library...

Representing optional values with std::optional

Although quite a minor feature from C++17, std::optional is a nice addition to the standard library. It simplifies a common case that couldn't be expressed in a clean and straightforward way prior to std::optional. In a nutshell, it is a small wrapper for any type where the wrapped type can be either initialized or uninitialized.

To put it in C++ lingo, std::optional is a stack-allocated container with a max size of one.

Optional return values

Before the introduction of std::optional, there was no clear way to define functions that may not return a defined value, such as the intersection point of two line segments. With the introduction of std::optional, such optional return values can be clearly expressed. What follows is an implementation of a function that returns an optional intersection between two lines:

// Prerequisite
struct Point { /* ......

Fixed size heterogenous collections

The C++ Utility library includes two class templates that can be used for storing multiple values of different types: std::pair and std::tuple. They are both collections with a fixed size. Just like std::array, it's not possible to add more values dynamically at runtime.

The big difference between std::pair and std::tuple is that std::pair can only hold two values, whereas std::tuple can be instantiated with an arbitrary size at compile time. We will begin with a brief introduction to std::pair before moving on to std::tuple.

Using std::pair

The class template std::pair lives in the <utility> header and has been available in C++ since the introduction of the standard template library. It is used in the standard library where algorithms need to return two values, such as std::minmax(), which can return both the smallest and the greatest value of an initializer list:

std::pair<int, int> v = std::minmax...

Dynamically sized heterogenous collections

We started this chapter by noting that the dynamically sized containers offered by C++ are homogenous, meaning that we can only store elements of one single type. But sometimes, we need to keep track of a collection that's dynamic in size that contains elements of different types. To be able to do that, we will use containers that contain elements of type std::any or std::variant.

The simplest solution is to use std::any as the base type. The std::any object can store any type of value in it:

auto container = std::vector<std::any>{42, "hi", true};

It has some drawbacks, though. First, every time a value in it is accessed, the type must be tested for at runtime. In other words, we completely lose the type information of the stored value at compile time. Rather, we have to rely on runtime type checks for the information. Secondly, it allocates the object on the heap rather than the stack, which can have significant...

Some real-world examples

We will end this chapter by examining two examples where std::tuple, std::tie(), and some template metaprogramming can help us to write clean and efficient code in practice.

Example 1: projections and comparison operators

The need to implement comparison operators for classes dramatically decreased with C++20, but there are still cases where we need to provide a custom comparison function when we want to sort objects in some custom order for a specific scenario. Consider the following class:

struct Player {
  std::string name_{};
  int level_{};
  int score_{};
  // etc...
};
auto players = std::vector<Player>{};
// Add players here...

Say that we want to sort the players by their attributes: the primary sort order level_ and the secondary sort order score_. It's not uncommon to see code like this when implementing comparison and sorting:

auto cmp = [](const Player& lhs, const Player& rhs) {
  if (lhs.level_ ==...

Summary

In this chapter you have learned how to use std::optional to represent optional values in your code. You have also seen how to combine std::pair, std::tuple, std::any, and std::variant together with standard containers and metaprogramming to store and iterate over elements of different types. You also learned that std::tie() is a conceptually simple yet powerful tool that can be used for projection and reflection.

In the next chapter, you will find out how to further expand your C++ toolbox to create libraries by learning how to construct hidden proxy objects.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
C++ High Performance - Second Edition
Published in: Dec 2020Publisher: PacktISBN-13: 9781839216541
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Authors (2)

author image
Björn Andrist

Björn Andrist is a freelance software consultant currently focusing on audio applications. For more than 15 years, he has been working professionally with C++ in projects ranging from UNIX server applications to real-time audio applications on desktop and mobile. In the past, he has also taught courses in algorithms and data structures, concurrent programming, and programming methodologies. Björn holds a BS in computer engineering and an MS in computer science from KTH Royal Institute of Technology.
Read more about Björn Andrist

author image
Viktor Sehr

Viktor Sehr is the founder and main developer of the small game studio Toppluva AB. At Toppluva he develops a custom graphics engine which powers the open-world skiing game Grand Mountain Adventure. He has 13 years of professional experience using C++, with real-time graphics, audio, and architectural design as his focus areas. Through his career, he has developed medical visualization software at Mentice and Raysearch Laboratories as well as real-time audio applications at Propellerhead Software. Viktor holds an M.S. in media science from Linköping University.
Read more about Viktor Sehr