Practical C++20

Practical C++20 is a two-day training course with programming examples taught by Jeff Garland. It is offered onsite at the Gaylord Rockies from 09:00 to 17:00 on Saturday, October 30th and Sunday, October 31st, 2021 (immediately after the conference). Lunch is included.

Course Description

C++20 is a huge release with many new features to learn making it daunting to get started. This tutorial will do a deep dive on concepts, ranges, span, spaceship, format, chrono, jthread, and other key C++20 library facilities from an application developers perspective. A combination of lecture and hands-on exercises provides the student a hands on working knowledge of the best of C++20. Targeted at intermediate to advanced developers this session expects no prior knowledge of C++20.

Day 1 is devoted to concepts. Over the course of the day we will climb up the ladder from the basics of concepts to writing and designing with concepts. First we cover the basics of concepts, the concept and requires keywords. Then we explore the application of concepts from a library user point of view. Including as a variable declaration, return values, and function parameters. Then we’ll survey and apply C++20 standard library concepts to typical development of a typical C++ class. Next the tutorial will dig into writing concepts including what makes a good concept. We will look at the details of how concepts are evaluated. Finally, we’ll tie our new knowledge and look at some ways concepts can impact C++ designs including CRTP and SOLID.

Day 2 is devoted to the new standard library facilities and how best to use them. Ranges algorithms, adapters, and views get a deep treatment. As part of an exercise we build custom view adapters so students can extend ranges for their own applications. Next we look at using span and how it can simplify and improve interfaces. Next we dive into using the spaceship operator and the provided standard library support. With format we look at the new facilities, the basics of use and how developers can extend the facilities to work well with their own types. For chrono we see the new API for working with dates and times including format for input and output. We then switch our attention to the concurrency facilities in C++20 with special attention on std::jthread, it’s difference to std::thread and how to apply it in practice. Finally, we wrap up with some smaller facilities that solve everyday issues like container erase algorithms and signed/unsigned comparison facilities.

Prerequisites

Targeted at intermediate to advanced developers this session expects no prior knowledge of C++20. Basic knowledge of templates is assumed.

Course Topics

Day 1: Concepts

  • Introduction
  • Concept basics
    • Why do we want concepts?
    • Why do we want concepts II?
    • Long history
    • What’s a concept?
    • Boolean predicate composition
    • Types versus concepts
    • Simple one parameter concept printable
    • Notable concept properties
  • Using concepts in code
    • What can we do, not do with a concept?
    • Where can we write <concept_name> auto ?
    • Overload resolution – constrain function parameter
    • Pointers and concepts
    • Use in if constexpr
    • Non-template member function of template class
    • Constraining existing templates
    • Using standard library concepts
  • Reading concepts
    • requires expression and requires clause
    • requires expression basics
    • requires expression more realistic example
    • Constraint composition
    • is_arithmetic
    • Ranges and concepts
  • Writing concepts
    • How constraints are evaluated
    • Good concepts and bad concepts
    • Evolving concepts
    • Concepts for sleep_for
    • Refactoring the sleep_for concept
  • Designing with concepts
    • What is design?
    • Parnas – information hiding (1972)
    • Coplien – multi-paradigm design (1999)
    • Concept serialization
  • Conclusions & resources
    • Is concepts everything we want?
    • Final thoughts
    • Papers, blogs, videos

Day 2: C++20 Library Features

  • Intro
  • Range basics
    • Range, range algorithms, views, adaptors
    • Mechanics – headers, namespaces
    • Why put this in std::ranges instead of just std
    • What’s a range?
    • Range algorithms
    • Views are ranges with ‘lazy evaluation’
    • Range adaptors -> views from ranges
    • Views and range algorithm example
    • Collections != views, but are ranges
    • No more for loops – for real?
  • Range algorithms details and survey
    • The range algorithms are familiar
    • Projection parameters
    • Algorithms cheatsheet
    • Exercise: range algorithm with projection parameters
  • Range views and view adaptor details
    • Range adaptor equivalence
    • Types in a view chain – under the hood
    • View types
  • Survey of views – by example
    • iota_view example
    • take_view example
    • join_view char string example
    • empty_view example
    • single_view example
    • split_view character string example
    • reverse_view example 2
    • transform_view example
    • drop_while example
    • Cheatsheet
    • Exercise: composing several views
  • Making a custom view
    • View interface design details
    • Avoiding the boilerplate
    • Exercise: make a custom view type
  • span
    • span construction
    • span for better function interfaces
    • span and const
    • basic public accessors
    • static and dynamic extent
    • spanstream (c++23)
    • exercise: using span with vector and ranges
  • Spaceship operator <=>
    • It’s not rocket science, but…
    • Comparison is harder than you think
    • Expression rewriting
    • Move over less, it’s all about equal now
    • Simplifying your code with spaceship
  • Output with std::format
    • Hello world
    • Indexed parameters
    • Simple escaping
    • Crazy town
    • Format specifiers
    • Rich language for fill, alignment, conversion
    • Floating point format
    • Locale support
    • Custom type usage
    • How to extend for custom types
    • fmt diagnostics/safety
    • Mismatched parameter count
    • Wrong format typing causes error, even when convertible
    • Format exercise – references and status
  • Chrono
    • chrono design fundamentals
    • Simple calculations
    • chrono error handling
    • other types year_month, gps_clock, weekday_indexed
    • chrono i/o
    • chrono format output
    • locale aware format output
    • IANA timezone database support
    • localized times
    • other C++20 chrono features
    • chrono exercise – references and status
  • Concurrency: threading and atomics
    • jthread and stop_token
      • thread gone wrong?
      • jthread instead
      • jthread cooperative shutdown
      • stop_source, stop_token, stop_callback
      • jthread cooperative shutdown
      • New waiting interfaces with stop_token
      • Exercise: write jthread example
    • Atomic shared_ptr and weak_ptr
    • atomic_ref
    • syncbuf and osyncstream overview
    • Misc smaller things
      • Uniform container erasure
        • erase and erase_if signatures
        • erase and erase_if scope
    • Safe signed and unsigned comparison
      • Why is this needed?
      • cmp_equal, cmp_less, cmp_greater
    • contains method for associative containers
      • contains method signatures
      • contains method signatures for unordered
    • source_location
      • source_location synopis
      • source_location usage
    • starts_with and ends_with for string and string_view
    • Math constants
      • new header <math> and namespace std::math
      • floating point types, nearest representable value
      • math constants example
    • stringstream and basic_stringbuf updates
      • changes to allow for control of buffer allocator
      • output interfaces to provide string_view from stringstream
      • stringstream to string_view example

Register Here

Course Instructor

Jeff Garland is the assistant chair of the WG21 Library Working Group, a Boost author, and a recurring conference speaker. He has been writing high performance, distributed C++ applications since the late 90’s in domains ranging from financial systems to satellite ground control. He holds a Masters in Computer Science from ASU and a Bachelor of Science in Systems Engineering from the University of Arizona. He is a software architect, principal engineer, and security champion with more than 15 years of experience in designing, writing and maintaining C++ code for fun and living. C++ consultant, trainer, conference speaker, and evangelist focused on Modern C++. His main areas of interest and expertise are code performance, low latency, stability, and security.