Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

std

c++20

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?

like image 314
FreelanceConsultant Avatar asked Oct 24 '25 12:10

FreelanceConsultant


1 Answers

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.

like image 178
teapot418 Avatar answered Oct 26 '25 03:10

teapot418



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!