Reputation: 4980
#include <iostream>
#include <ranges>
int main()
{
for (int num: std::ranges::views::iota(0,5)) {
std::cout << num << 'n';
}
for (int i : std::ranges::iota_view{55, 65}) {
std::cout << i << '\n';
}
}
views::iota(e)
andviews::iota(e, f)
are expression-equivalent toiota_view(e)
andiota_view(e, f)
respectively for any suitable subexpressionse
andf
.
Is there any reason to use one vs the other?
And if not, why do they both exist since they were both introduced in C++20?
Upvotes: 5
Views: 1353
Reputation: 13434
std::ranges::iota_view
is the type that behaves like a view that generates its elements ad hoc. It must be somehow constructible.
std::views::iota
is a helper customization point object that follows a pattern which can yield more performant / correct results when used. It will ultimately return std::ranges::iota_view
. Said pattern is the usage of views::something
rather than ranges::something_view
.
When you nest more adaptors together, using the latter may turn out to produce better results (mainly in terms of compile-time).
In general, prefer views::meow
.
Upvotes: 8