8.15 — Global random numbers (Random.h)
What happens if we want to use a random number generator in multiple functions or files? One way is to create (and seed) our PRNG in our main() function, and then pass it everywhere we need it. But...
View Article11.x — Chapter 11 summary and quiz
Nice work. Function templates may seem pretty complex, but they are a very powerful way to make your code work with objects of different types. We’ll see a lot more template stuff in future chapters,...
View Article16.5 — Returning std::vector, and an introduction to move semantics
When we need to pass a std::vector to a function, we pass it by (const) reference so that we do not make an expensive copy of the array data. Therefore, you will probably be surprised to find that it...
View ArticleB.5 — Introduction to C++23
What is C++23? In February of 2023, the ISO (International Organization for Standardization) approved a new version of C++, called C++23. New improvements in C++23 For your interest, here’s a list of...
View Article12.15 — std::optional
In lesson 9.4 -- Detecting and handling errors, we discussed cases where a function encounters an error that it cannot reasonably handle itself. For example, consider a function that calculates and...
View Article13.4 — Converting an enumeration to and from a string
In the prior lesson (13.3 -- Unscoped enumerator integral conversions), we showed an example like this: #include <iostream> enum Color { black, // 0 red, // 1 blue, // 2 }; int main() { Color...
View Article13.5 — Introduction to overloading the I/O operators
In the prior lesson (13.4 -- Converting an enumeration to and from a string), we showed this example, where we used a function to convert an enumeration into an equivalent string: #include...
View Article0.13 — What language standard is my compiler using?
The following program is designed to print the name of the language standard your compiler is currently using. You can copy/paste, compile, and run this program to validate that your compiler is using...
View Article14.17 — Constexpr aggregates and classes
In lesson 5.8 -- Constexpr and consteval functions, we covered constexpr functions, which are functions that may be evaluated at either compile-time or runtime. For example: #include <iostream>...
View Article11.8 — Using function templates in multiple files
Consider the following program, which doesn’t work correctly: main.cpp: #include <iostream> template <typename T> T addOne(T x); // function template forward declaration int main() {...
View Article