The following code example is taken from the book
C++23 - The Complete Guide
by Nicolai M. Josuttis,
Leanpub, 2026
The code is licensed under a
Creative Commons Attribution 4.0 International License.
// raw code
#include <filesystem>
#include <generator>
#include "tracingpmr.hpp"
std::pmr::generator<std::string> allFiles(const std::filesystem::path& p)
{
if (is_directory(p)) { // if path p is a directory
// iterate with a coroutine over the subdirectory:
for (const auto& e : std::filesystem::directory_iterator{p}) {
co_yield std::ranges::elements_of(allFiles(e.path()));
}
}
else {
// yield the normalized filename:
co_yield p.lexically_normal().string();
}
}