user2138149
user2138149

Reputation: 17266

How do I control the type of value returned by std::ranges::iota_view?

C++ 20 introduces std::ranges::iota_view.

It may be used as part of a for loop, for example.

for (auto i: std::ranges::iota_view(0, 10))

It appears to take two template parameters. For example, the following will compile.

std::ranges::iota_view<int64_t, int64_t>(0, 10)

The first template parameter is W which should be std::weakly_incrementable. The second template parameter is Bound which should be std::semiregular.

What is the purpose of these two template parameters? Can they be used to control the type returned by iota_view?

In other words, what effect, if any, do they have on the deduced type of auto i in the example above?

Upvotes: 4

Views: 92

Answers (1)

teapot418
teapot418

Reputation: 2578

https://en.cppreference.com/w/cpp/ranges/iota_view <- we start at template<W, Bound> class iota_view.

Its iterator is std::ranges::iota_view<W, Bound>::iterator

And operator * on this iterator returns a value of type W. So the value type for the for loop is controlled by the first template parameter.

The Bound type is checked to be comparable ("weakly-equality-comparable-with") with W.

Upvotes: 6

Related Questions