13.9 — Struct miscellany

Structs with program-defined members In C++, structs (and classes) can have members that are other program-defined types. There are two ways to do this. First, we can define one program-defined type (in the global scope) and then use it as a member of another program-defined type: #include <iostream> struct Employee …

8.4 — Constexpr if statements

Normally, the conditional of an if-statement is evaluated at runtime. However, consider the case where the conditional is a constant expression, such as in the following example: #include <iostream> int main() { constexpr double gravity{ 9.8 }; // reminder: low-precision floating point literals of the same type can be tested …

10.4 — Narrowing conversions, list initialization, and constexpr initializers

In the previous lesson (), we covered numeric conversions, which cover a wide range of different type conversions between fundamental types. Narrowing conversions In C++, a is a potentially unsafe numeric conversion where the destination type may not be able to hold all the values of the source type. The …

21.y — Chapter 21 project

A tip o’ the hat to reader Avtem for conceiving of and collaborating on this project. Project time Let’s implement the classic game 15 Puzzle! In 15 Puzzle, you begin with a randomized 4×4 grid of tiles. 15 of the tiles have numbers 1 through 15. One tile is missing. …

13.12 — 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{ …