You already know that using lazy evaluation in std::ranges::views can help with performance by eliminating unnecessary compute. It turns out we can also use ranges to reduce the memory overhead in our example. Let's revisit our code for obtaining featured items from a store. It can be shortened down to the following:
auto get_featured_items_for_store(const Store &store) { return store.items | views::filter(&Item::featured) | views::transform( [](const auto &item) { return gsl::not_null(&item); }); }
Note that our function no longer returns items, instead relying on C++14's auto return type deduction. In our case, instead of returning a vector, our code will return a lazy view.
Let's learn how to consume this for all stores:
Items get_all_featured_items(const Stores &stores) { auto all_featured = stores | views::transform([](auto elem) { ...