CodeTheRobot

Track 02 · Intermediate · Free

C++ Intermediate

Modern C++17, templates and CMake: the heart of ROS 2 C++.

Phase 3 teaches modern C++ (C++11 to C++17): smart pointers, move semantics, lambdas, the STL, threads and the C++17 vocabulary types. Phase 4 covers templates and generic programming. Phase 5 covers CMake, gdb and clang-tidy, which every ROS 2 package depends on.

Prerequisite: C++ Beginner, or comfort with classes, pointers, references and virtual functions.

Pixel-art C++ robotics programming workbench
COMPILER READY
Track 02 · Intermediate TRACK

You will be able to

  • Own memory with unique_ptr, shared_ptr and move semantics
  • Use lambdas, STL containers, algorithms, threads and futures
  • Write templates and choose code by type with if constexpr
  • Build, debug and analyse a multi-target project with CMake, gdb and clang-tidy

Lesson outline

01

auto and type inference

How the compiler deduces types, auto with references and const, and structured bindings.

45–60 minOpen lesson
02

Smart pointers: std::unique_ptr

Exclusive ownership, make_unique, transferring ownership and returning from functions.

45–75 minOpen lesson
03

Smart pointers: std::shared_ptr and std::weak_ptr

Shared ownership, reference counts, weak observers and breaking cycles.

60–90 minOpen lesson
04

Move semantics: move constructor, move assignment and std::move

Transferring resources instead of copying, and the rule of five.

60–90 minOpen lesson
05

Lambdas: capture lists and inline callbacks

Anonymous functions, [=] vs [&] vs explicit captures, and lambdas as callbacks.

60–90 minOpen lesson
06

STL containers: vector, map, unordered_map, list

Choosing between the standard containers and their costs.

60–90 minOpen lesson
07

STL algorithms: sort, find and for_each

Expressing intent with <algorithm> instead of hand-written loops.

45–75 minOpen lesson
08

Iterators: input, output, forward, bidirectional and random-access

The five iterator categories and what each one allows.

45–75 minOpen lesson
09

std::chrono: timing and durations

Clocks, time points, durations, and typed time literals.

45–60 minOpen lesson
10

Multithreading: std::thread, std::mutex and std::lock_guard

Starting threads, joining them, data races and protecting shared data.

60–90 minOpen lesson
11

Async programming: std::future and std::async

Running work in the background and collecting the result later.

45–75 minOpen lesson
12

std::optional and std::variant

Values that may be missing, and values that are one of several types.

45–75 minOpen lesson
13

std::function

Storing any callable, callback members and the cost of type erasure.

45–60 minOpen lesson
14

Type traits

std::is_same, std::is_integral, enable_if and compile-time type questions.

45–75 minOpen lesson
15

constexpr (C++17)

Computing values at compile time, constexpr functions and constexpr if.

45–60 minOpen lesson
16

Function templates

Type parameters, deduction, explicit arguments and when templates fail to compile.

45–75 minOpen lesson
17

Class templates

Generic containers and value types, template arguments and class template deduction (C++17).

60–90 minOpen lesson
18

Template specialization

Full and partial specialization, and overriding the generic behaviour for one type.

45–75 minOpen lesson
19

SFINAE basics

Substitution failure is not an error: enable_if and choosing overloads by type.

60–90 minOpen lesson
20

Generic programming patterns

Policies, tag dispatch, CRTP and writing small generic algorithms.

60–90 minOpen lesson
21

Basic CMakeLists

cmake_minimum_required, project, add_executable and an out-of-source build.

45–75 minOpen lesson
22

Targets, linking and include directories

Libraries as targets, target_link_libraries and PUBLIC vs PRIVATE.

60–90 minOpen lesson
23

Static vs shared libraries

add_library STATIC and SHARED, position-independent code, and runtime paths.

45–75 minOpen lesson
24

Organizing multi-file projects

Folder layout, add_subdirectory, tests and a public include tree.

60–90 minOpen lesson
25

Using external libraries

find_package, imported targets, FetchContent and pkg-config.

60–90 minOpen lesson
26

Debugging with gdb

Debug builds, breakpoints, stepping, backtraces and watching variables.

60–90 minOpen lesson
27

clang-tidy and static analysis

Finding bugs without running the code, and using compile_commands.json.

45–75 minOpen lesson

Project examples

PRJ-04

Fixed-capacity telemetry ring

A ring buffer class template holding any sample type, protected by a mutex so a producer thread and a consumer thread can share it safely.

template <typename T, std::size_t N>
class RingBuffer {
  std::array<T, N> data_{};
public:
  bool push(const T& v);
};
PRJ-05

Owned device handle

Wrap a fake serial device in a move-only RAII type that can never leak or double-close its handle, and transfer it between owners with std::move.

class Device {
  int fd_{-1};
public:
  Device(Device&& o) noexcept : fd_{std::exchange(o.fd_, -1)} {}
  ~Device() { if (fd_ >= 0) ::close(fd_); }
};
PRJ-06

Generic trajectory interpolator

One templated interpolator that works for scalars, 2D points and 6-axis joint vectors, built as a CMake library with unit tests.

template <typename P>
P lerp(const P& a, const P& b, double t) {
  return a + (b - a) * t;
}

Free course

Begin when you are ready.

27 lessons · 3 phases · self-paced. Curriculum aligned with the ISO C++ reference material and verified September 2026.

ISO C++ resources