Home > database >  C : Alternative to this range's view construction with same requirements?
C : Alternative to this range's view construction with same requirements?

Time:07-06

To remove manual logic in my code, I use that construct:

std::ranges::drop_view a { std::ranges::take_view(my_range, my_range.size() -X), Y};

with X and Y values I pass at runtime.

Even though I check the algorithms, I could not find a shorter way that has the following constraints:

  1. don't go beyond or below the range I want, and don't do anything if the range has 0 elements -> no overflow
  2. non owning -> no copies

ranges::subranges doesn't meet those requirements.

Thanks

CodePudding user response:

You can compose take_view and drop_view into a new adaptor

auto take_and_drop = [](auto n, auto m) {
  return std::views::take(n) | std::views::drop(m);
};

auto a = my_range | take_and_drop(my_range.size() - X, Y);
  • Related