12.15 — std::optional

In lesson , we discussed cases where a function encounters an error that it cannot reasonably handle itself. For example, consider a function that calculates and returns a value: int doIntDivision(int x, int y) { return x / y; } If the caller passes in a value that is semantically …

5.3 — Constexpr variables

The constexpr keyword When you declare a const variable using the const keyword, the compiler will implicitly keep track of whether it’s a runtime or compile-time constant. In most cases, this doesn’t matter for anything other than optimization purposes, but there are a few cases where C++ requires a constant …

10.13 — Deleting functions

In some cases, it is possible to write functions that don’t behave as desired when called with values of certain types. Consider the following example: #include <iostream> void printInt(int x) { std::cout << x << ‘\n’; } int main() { printInt(5); // okay: prints 5 printInt(‘a’); // prints 97 — …

15.10 — Ref qualifiers

Author’s note This is an optional lesson. We recommend having a light read-through to familiarize yourself with the material, but comprehensive understanding is not required to proceed with future lessons. In lesson , we discussed how calling access functions that return references to data members can be dangerous when the …

14.7 — Member functions returning references to data members

In lesson , we covered return by reference. In particular, we noted, “The object being returned by reference must exist after the function returns”. This means we should not return local variables by reference, as the reference will be left dangling after the local variable is destroyed. However, it is …

13.13 — Alias templates

In lesson , we discussed how type aliases let us define an alias for an existing type. Creating a type alias for a class template where all template arguments are explicitly specified works just like a normal type alias: #include <iostream> template <typename T> struct Pair { T first{}; T …