Search

7.14 — Common semantic errors in C++

In lesson 3.1 — Syntax and semantic errors, we covered syntax errors, which occur when you write code that is not valid according to the grammar of the C++ language. The compiler will notify of you these types of errors, so they are trivial to catch, and usually straightforward to fix.

We also covered semantic . . . → Read More: 7.14 — Common semantic errors in C++

7.13 — Code coverage

In the previous lesson 7.12 — Introduction to testing your code, we discussed how to write and preserve simple tests. In this lesson, we’ll talk about what kind of tests are useful to write to ensure your code is correct.

Code coverage

The term code coverage is used to describe how . . . → Read More: 7.13 — Code coverage

7.11 — Halts (exiting your program early)

The last category of flow control statement we’ll cover is the halt. A halt is a flow control statement that terminates the program. In C++, halts are implemented as functions (rather than keywords), so our halt statements will be function calls.

Let’s take a brief detour, and recap what happens when a program exits normally. . . . → Read More: 7.11 — Halts (exiting your program early)

7.5 — Switch fallthrough and scoping

This lesson continues our exploration of switch statements that we started in the prior lesson 7.4 — Switch statement basics. In the prior lesson, we mentioned that each set of statements underneath a label should end in a break statement or a return statement.

In this lesson, we’ll explore why, and talk about some switch . . . → Read More: 7.5 — Switch fallthrough and scoping

7.3 — Common if statement problems

This lesson is a continuation of lesson 7.2 — If statements and blocks. In this lesson, we’ll take a look at some common problems that occur when using if statements.

Nested if statements and the dangling else problem

It is possible to nest if statements within other if statements:

The . . . → Read More: 7.3 — Common if statement problems

M.5 — std::move_if_noexcept

(h/t to reader Koe for providing the first draft of this lesson!)

In lesson 20.9 — Exception specifications and noexcept, we covered the noexcept exception specifier and operator, which this lesson builds on.

We also covered the strong exception guarantee, which guarantees that if a function is interrupted by an exception, no memory will be . . . → Read More: M.5 — std::move_if_noexcept

20.9 — Exception specifications and noexcept

(h/t to reader Koe for providing the first draft of this lesson!)

In C++, all functions are classified as either non-throwing (do not throw exceptions) or potentially throwing (may throw an exception).

Consider the following function declaration:

Looking at a typical function declaration, it is not possible to determine whether a function might throw . . . → Read More: 20.9 — Exception specifications and noexcept

8.1 — Using a language reference

Depending on where you’re at in your journey with learning programming languages (and specifically, C++), LearnCpp.com might be the only resource you’re using to learn C++ or to look something up. LearnCpp.com is designed to explain concepts in a beginner-friendly fashion, but it simply can’t cover every aspect of the language. As you begin to . . . → Read More: 8.1 — Using a language reference

8.x — Chapter 8 summary and quiz

Quick review

std::string offers an easy and safe way to deal with text strings. String literals are always placed between double quotes.

Enumerated types let us define our own type where all of the possible values are enumerated. These are great for categorizing things.

Enum classes work like enums but offer more type safety . . . → Read More: 8.x — Chapter 8 summary and quiz

6.17 — Unnamed and inline namespaces

C++ supports two permutations on regular namespaces that are worth at least knowing about. We won’t build on these, so consider this lesson optional for now.

Unnamed (anonymous) namespaces

An unnamed namespace (also called an anonymous namespace) is a namespace that is defined without a name, like so:

This prints:

. . . → Read More: 6.17 — Unnamed and inline namespaces