The ranges library in C++20 supports the expression
auto view = r | std::views::drop(n);
to remove the first n
elements of a range r
with the range adaptor drop
.
However if I recursively drop elements from a range, the compiler enters an infinite loop.
Minimal working example: (takes infinite time to compile in GCC 10)
#include <ranges>
#include <iostream>
#include <array>
#include <string>
using namespace std;
template<ranges::range T>
void printCombinations(T values) {
if(values.empty())
return;
auto tail = values | views::drop(1);
for(auto val : tail)
cout << values.front() << " " << val << endl;
printCombinations(tail);
}
int main() {
auto range1 = array<int, 4> { 1, 2, 3, 4 };
printCombinations(range1);
cout << endl;
string range2 = "abc";
printCombinations(range2);
cout << endl;
}
expected output:
1 2
1 3
1 4
2 3
2 4
3 4
a b
a c
b c
Why does this take infinite time to compile and how should I resolve the problem?