I am trying to learn std::ranges
library that is introduced in C++20 but I'm unable to get it working even for this small example.
#include <iostream>
#include <vector>
#include <ranges>
template<typename View>
requires std::ranges::view<View>
void print(View v)
{
if (v.empty())
{
std::cout << "\n";
return;
}
std::cout << v[0] << " ";
print(v | std::views::drop(1));
}
int main()
{
std::vector<int> v{1, 2, 3, 4};
print(v | std::views::all);
}
You can check at godbolt that it does not compile.
In my local setup I get around 1000 lines long error message about recursive instantiation of templates.
I know I can do it iteratively, but that's not the point, I want to know why this is not working and how can I make it work. Thanks.