## 4.14 — Compile-time constants, constant expressions, and constexpr

Consider the following short program: #include <iostream> int main() { std::cout << 3 + 4; return 0; } The output is straightforward: 7 However, there’s an interesting optimization possibility hidden within. If this program were compiled as it was written, the compiler would generate an executable that calculates the result …

## 4.16 — Numeral systems (decimal, binary, hexadecimal, and octal)

Author’s note This lesson is optional. Future lessons reference hexadecimal numbers, so you should at least have a passing familiarity with the concept before proceeding. In everyday life, we count using numbers, where each numerical digit can be 0, 1, 2, 3, 4, 5, 6, 7, 8, or 9. Decimal …

## 11.7 — std::string_view (part 2)

Author’s note Some of the content of this lesson was moved into the introduction to std::string_view lesson (). As a result, this lesson contains some duplicative content that has not been cleaned up yet. This will be addressed when this chapter is rewritten (soon). In the previous lesson, we talked …

## 10.11 — Class template argument deduction (CTAD) and deduction guides

Class template argument deduction (CTAD) C++17 Starting in C++17, when instantiating an object from a class template, the compiler can deduce the template types from the types of the object’s initializer (this is called or for short). For example: #include <utility> // for std::pair int main() { std::pair<int, int> p1{ …

## 10.10 — Class templates

In lesson , we introduced the challenge of having to create a separate (overloaded) function for each different set of types we want to work with: #include <iostream> // function to calculate the greater of two int values int max(int x, int y) { return (x > y) ? x …

## 2.3 — Void functions (non-value returning functions)

In a prior lesson (), we indicated that the syntax for a function definition looks like this: return-type identifier() // identifier replaced with the name of your function { // Your code here } Although we showed examples of functions that had return-type void, we did not discuss what this …

## 9.x — Chapter 9 summary and quiz

Quick review (also called ) are data types that can be constructed from fundamental data types (or other compound data types). The of an expression indicates whether an expression resolves to a value, a function, or an object of some kind. An is an expression that evaluates to a function …

## A.4 — C++ FAQ

There are certain questions that tend to get asked over and over. This FAQ will attempt to answer the most common ones. Q: Why shouldn’t we use “using namespace std”? The statement using namespace std; is a . Using directives import all of the identifiers from a namespace into the …

## 6.14 — Constexpr and consteval functions

In lesson , we introduced the constexpr keyword, which we used to create compile-time (symbolic) constants. We also introduced constant expressions, which are expressions that can be evaluated at compile-time rather than runtime. Consider the following program, which uses two constexpr variables: #include <iostream> int main() { constexpr int x{ …

## 7.18 — Introduction to random number generation

The ability to generate random numbers can be useful in certain kinds of programs, particularly in games, statistical modelling programs, and cryptographic applications that need to encrypt and decrypt things. Take games for example — without random events, monsters would always attack you the same way, you’d always find the …