Module 9 · Modern C++ — topic 3 of 3
Modern C++ Fundamentals
The features that define idiomatic C++11 to C++20: auto, range-for, move semantics, constexpr, structured bindings, optional, variant, string_view, enum class, and a look at C++20 ranges and concepts.
Learning objectives
- Use enum class, std::optional and std::variant
- Explain move semantics at a practical level
- Recognise C++20 ranges and concepts
Definition
Modern C++ refers to the language and library features introduced from C++11 onward and the idioms built on them — value semantics, RAII, type deduction and expressive, safe abstractions with no run-time overhead.
Introduction
C++ in 2026 looks very different from the C++ of 1998. Modern features replace error-prone patterns: enum class instead of plain enums, std::optional instead of magic "not found" values, std::variant instead of unsafe unions, and smart pointers instead of manual delete.
Why it matters
Industry codebases and interviews expect modern idioms.
These features eliminate whole categories of bugs while keeping performance.
Detailed explanation
enum class (C++11): scoped, strongly typed enumerations — Color::Red — that don't convert implicitly to int.
Move semantics: objects can transfer their resources instead of copying (std::move). Returning containers by value is cheap.
std::optional<T> (C++17): either a T or nothing; replaces sentinel values like -1. std::variant<A, B>: a type-safe union that knows which type it holds; visit it with std::visit.
std::string_view (C++17): non-owning view of characters for read-only parameters.
C++20 ranges: std::ranges::sort(v), views like v | std::views::filter(pred) | std::views::transform(f) compose lazily. Concepts constrain templates: template <std::integral T>.
Syntax
value_or(default)reads an optional with a fallback.std::holds_alternative<T>(var)checks a variant's type.- Compile with -std=c++20 for ranges/concepts.
Code examples
enum class and optional
2 found at 1 999
std::variant with std::visit
42 3.5 hello 1
More examples
Move semantics in action
copy b move b 3 trackers
Step-by-step explanation
emplace_backbuilds the element inside the vector — no copy or move printed.push_back(b)copies becausebis an lvalue.push_back(std::move(b))moves: the string's buffer is transferred.- Returning
vuses move/elision, so no element copies happen;reserveprevented reallocation moves.
Expected output
Running “Move semantics in action” prints exactly:
Practical applications
- Configuration parsers return optional values.
- JSON libraries represent values with variants.
- High-performance code relies on moves to avoid copying buffers.
Best practices
- Use enum class for all new enumerations.
- Return std::optional instead of sentinel values.
- Prefer value semantics; let moves make them cheap.
Common mistakes
Dereferencing an empty optional
Avoid
Prefer
* on an empty optional is undefined behaviour; value() would throw.
Using a moved-from object as if unchanged
Avoid
Prefer
After a move, the source is valid but unspecified; only assign to it or destroy it.
Important notes
- Ranges and concepts need -std=c++20; this course's examples compile as C++17 unless stated.
std::expected<T, E>(C++23) returns either a value or an error.
Key takeaways
- enum class is scoped and type-safe.
- optional and variant model 'maybe' and 'one of'.
- Moves transfer resources instead of copying.
Try it yourself
- 1Rewrite a function that returns -1 for 'not found' to return std::optional.
- 2Model a shape as std::variant<Circle, Square> and compute areas with std::visit.
Finished reading? Mark it complete to update your dashboard.
Suggested practice
5 questions test this exact topic. Answers and explanations are revealed after you submit.
- BeginnerWhat advantage does `enum class Color { Red };` have over a plain enum?
- BeginnerWhat is printed?
- ModerateWhat is printed?