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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | #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; } } |
18 19 20 21 22 23 24 25 26 27 | 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.