Reputation: 42776
Consider the following case:
std::vector<int> v{0, 1, 2, 3, 4, 5};
// 0 1 2 3 4 5
auto rng1 = std::views::all(v);
// 5 4 3 2 1 0
auto rng2 = std::views::reverse(v);
// 4 2 0
auto rng3 = std::views::filter(rng2, [](int x){return x % 2 == 0;});
Is there a elegant way to concatenate those three adaptors into one single view like this:
// 0 1 2 3 4 5 5 4 3 2 1 0 4 2 0
auto final_rng = std::views::concat(rng1, rng2, rng3);
It seems impossible since rng1
, rng2
, and rng3
's are very different types.
Can someone give an alternative solution? thanks.
Upvotes: 12
Views: 7317
Reputation: 10526
There is actually a concat() in the standard coming with C++26. Check out the spec:
https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2023/p2542r7.html
As of today (June 2024), it is implemented in g++-15.
Upvotes: 5
Reputation: 303057
Yes, what you wrote just in a different namespace - there's no concat in the Standard Library but there is one in range-v3:
auto final_rng = ranges::views::concat(rng1, rng2, rng3);
The fact that the ranges are different types doesn't pose a problem. You just have an iterator that's built up of a variant of the underlying iterators of the ranges. The important part is that the value type of the ranges is the same - and here it is, so that's totally fine.
Upvotes: 6