Quantcast
Channel: Learn C++
Browsing all 16 articles
Browse latest View live

9.x — Chapter 9 summary and quiz

Chapter Review Scope creep occurs when a project’s capabilities grow beyond what was originally intended at the start of the project or project phase. Software verification is the process of testing...

View Article


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 Article


11.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 Article

16.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 Article

B.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 Article


12.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 Article

13.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 Article

13.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 Article


0.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 Article


14.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 Article

11.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

5.4 — The as-if rule and compile-time optimization

Introduction to optimization In programming, optimization is the process of modifying software to make it work more efficiently (e.g. to run faster, or use fewer resources). Optimization can have a...

View Article

F.2 — Constexpr functions (part 2)

Constexpr function calls in non-required constant expressions You might expect that a constexpr function would evaluate at compile-time whenever possible, but unfortunately this is not the case. In...

View Article


F.3 — Constexpr functions (part 3) and consteval

Forcing a constexpr function to be evaluated at compile-time There is no way to tell the compiler that a constexpr function should prefer to evaluate at compile-time whenever it can (e.g. in cases...

View Article

F.4 — Constexpr functions (part 4)

Constexpr/consteval functions can use non-const local variables Within a constexpr or consteval function, we can use local variables that are not constexpr, and the value of these variables can be...

View Article


F.X — Chapter F summary and quiz

A constexpr function is a function that is allowed to be called in a constant expression. To make a function a constexpr function, we simply use the constexpr keyword in front of the return type....

View Article
Browsing all 16 articles
Browse latest View live