I’m attempting to understand how views can be composed and the limitations of the same.
The idea here is to split a string
/string_view
without creating a temporary variable as shown in the second code block.
Why does the following not compile:
std::string line = "1[2]3";
for (const auto split : line
| std::ranges::views::split('[')
| std::ranges::views::transform([](const auto rng) {return std::string_view(rng); }))
| std::ranges::views::split(']')
| std::ranges::views::transform([](const auto rng) {return std::string_view(rng); }))
std::cout << split << 'n';
}
An additional question is why the above code fails to compile but the following work:
for (const auto split_once : line
| std::ranges::views::split('[')
| std::ranges::views::transform([](const auto rng) {return std::string_view(rng); }))
{
for (const auto split_twice : split_once
| std::ranges::views::split(']')
| std::ranges::views::transform([](const auto rng) {return std::string_view(rng); }))
{
std::cout << split_twice << 'n';
}
}
Link to compiler explorer