views::iota vs ranges::iota_view - "expression equivalent" so why both exist?
Asked Answered
H

1

5
#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';
    }
}

CPP Reference

  1. views::iota(e) and views::iota(e, f) are expression-equivalent to iota_view(e) and iota_view(e, f) respectively for any suitable subexpressions e and f.

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?

Honolulu answered 13/7, 2023 at 19:20 Comment(0)
P
8

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.

Prostomium answered 13/7, 2023 at 19:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.