Book Image

C++ High Performance - Second Edition

By : Björn Andrist, Viktor Sehr
5 (2)
Book Image

C++ High Performance - Second Edition

5 (2)
By: Björn Andrist, Viktor Sehr

Overview of this book

C++ High Performance, Second Edition guides you through optimizing the performance of your C++ apps. This allows them to run faster and consume fewer resources on the device they're running on without compromising the readability of your codebase. The book begins by introducing the C++ language and some of its modern concepts in brief. Once you are familiar with the fundamentals, you will be ready to measure, identify, and eradicate bottlenecks in your C++ codebase. By following this process, you will gradually improve your style of writing code. The book then explores data structure optimization, memory management, and how it can be used efficiently concerning CPU caches. After laying the foundation, the book trains you to leverage algorithms, ranges, and containers from the standard library to achieve faster execution, write readable code, and use customized iterators. It provides hands-on examples of C++ metaprogramming, coroutines, reflection to reduce boilerplate code, proxy objects to perform optimizations under the hood, concurrent programming, and lock-free data structures. The book concludes with an overview of parallel algorithms. By the end of this book, you will have the ability to use every tool as needed to boost the efficiency of your C++ projects.
Table of Contents (17 chapters)
15
Other Books You May Enjoy
16
Index

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