June 2025
Intermediate to advanced
1093 pages
33h 24m
English
If you want to write algorithms that behave differently depending on the type of range (or container) they operate on, starting with C++20, you can use constexpr if and the well-thought-out hierarchy of ranges concepts to write the appropriate implementation in the different if constexpr branches.
Within a single function, you can decide which algorithm you want to use for which range. All of this happens at compile time, as shown in Listing 26.4.
// https://godbolt.org/z/rqz1s6h11#include <iostream>#include <vector>#include <list>#include <ranges>namespace rs = std::ranges;template<rs::range R>void alg(R&& range) { if constexpr(rs::random_access_range<R>) std::cout ...
Read now
Unlock full access