C++ · Advanced level

Advanced level · Module 3 of 6 · 20 guided hours

Modern C++17 and C++20 Features

Use the language and library features that make modern C++ clearer and safer: auto, structured bindings, optional, variant, string_view, constexpr and modern error handling.

Learning objectives

  • Use type deduction and structured bindings appropriately
  • Represent optional and alternative values with optional and variant
  • Use string_view, constexpr and modern formatting

Topics

  • auto and structured bindings
  • std::optional
  • std::variant and std::visit
  • std::string_view
  • constexpr and consteval
  • std::format
  • error-handling strategies

Lessons

  1. Lesson 1 auto, Structured Bindings and if-initialisers

    Use type deduction where it improves readability, unpack pairs and structs, and keep variables in the narrowest scope.

    autostructured bindingsif with initialiserrange-for over maps

  2. Lesson 2 optional and variant

    Represent 'maybe a value' with std::optional and 'one of several types' with std::variant instead of special values.

    std::optionalvalue_orstd::variantstd::visit

  3. Lesson 3 string_view, constexpr and std::format

    Pass text efficiently with string_view, compute values at compile time with constexpr and format output with std::format.

    std::string_viewdangling viewconstexpr / constevalstd::format

  4. Lesson 4 Choosing an Error-Handling Strategy

    Compare exceptions, optional/variant results, error codes and assertions, and choose consistently for a code base.

    exceptionsresult typeserror codesassert and contracts

Practical activity

Rewrite a sensor-record parser to return std::optional or a variant result instead of special values, and format its output with std::format.

Expected outcomes

  • Clearer code with fewer special values and casts
  • Informed choices between error-handling approaches

Assessment

Code review of the rewritten parser plus a short written comparison of exceptions, optional and variant results.

Taught live by an instructor — online or in person. Free course lessons are for self-study and do not lead to a certificate — see registration options.