5.5 — Constant expressions

In lesson , we introduced expressions. By default, expressions evaluate at runtime. And in some cases, they must do so: std::cin >> x; std::cout << 5 << ‘\n’; Because input and output can’t be performed at compile time, the expressions above are required to evaluate at runtime. In prior lesson …

5.9 — std::string_view (part 2)

In prior lessons, we introduced two string types: std::string () and std::string_view (). Because std::string_view is our first encounter with a view type, we’re going to spend some additional time discussing it further. We will focus on how to use std::string_view safely, and provide some examples illustrating how it can …

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 …

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. Q1: Why shouldn’t we use “using namespace std”? The statement using namespace std; is a . A using-directive allows all of the identifiers from a given namespace to …

F.1 — Constexpr 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. One challenge with constant expressions is that function call to a normal function are not allowed in …