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.14 — 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{ …

13.13 — 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) ? y …