Debug

In the member function version, the expression cents1 + 2 becomes function call cents1.operator+(2). Note that there is now only one explicit function parameter, and cents1 has become an object prefix. However, in lesson , we mentioned that the compiler implicitly converts an object prefix into a hidden leftmost parameter …

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

10.10 — 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) ? x …

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. Q: Why shouldn’t we use “using namespace std”? The statement using namespace std; is a . Using directives import all of the identifiers from a namespace into the …

6.14 — Constexpr and consteval 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. Consider the following program, which uses two constexpr variables: #include <iostream> int main() { constexpr int x{ …

7.18 — Introduction to random number generation

The ability to generate random numbers can be useful in certain kinds of programs, particularly in games, statistical modelling programs, and cryptographic applications that need to encrypt and decrypt things. Take games for example — without random events, monsters would always attack you the same way, you’d always find the …