c++c++20range-v3std-ranges

Concatenate multiple range adaptors into one single ranges in C++20


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.

Upate:

C++26 finally introduced views::concat, so the example in my question will work perfectly in C++26 (Demo).


Solution

  • 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.