How we use forwarding references with ranges in C++

The parameter or print function is a forwarding reference because the range can change while being iterated, but the current value is l-value reference:

#include <string>
#include <vector>
#include <ranges>
#include <iostream>

template <class R, class Value>
concept range_over = std::ranges::range<R> &&
    std::same_as<std::ranges::range_value_t<R>, Value>;

template <range_over<std::string> Range>
void print(Range&& range)
{
    for (const std::string& val : range)
    {
        std::cout << val << std::endl;
    }
}
int main()
{
    std::vector<std::string> v = { "0abc", "1def", "2ghi", "1jkl" };
 
    print(v);

    print(v | std::views::filter([](const std::string& val) { return val.starts_with('1'); }));

    return 0;
}

We call print with both a named variable and a temporary.

Leave a Reply

Your email address will not be published. Required fields are marked *