Search code examples
c++c++20clionc++-conceptsstd-ranges

weakly_incrementable constraint while using iota_view


To quickly create a for loop similar to python's for i in range(100), I could do:

for (auto const i : std::views::iota(0, 100))
{ /* ... */ }

However, CLion is warning me to add a std::weakly_incrementable constraint to my auto:

for (std::weakly_incrementable auto const i : std::views::iota(0, 100))
{ /* ... */ }

I know that the starting value of iota_view must be weakly_incrementable, and I would assume that auto i will create an i that is the same type of the starting value.

But is it true? Is it possible for it to create something of a totally different type (not even weakly incrementable)? And can I safely ignore the warning?


Solution

  • Is it possible for it to create something of a totally different type (not even weakly incrementable)? And can I safely ignore the warning?

    According to the synopsis of iota_view in [range.iota.view]:

    template<weakly_­incrementable W, semiregular Bound = unreachable_sentinel_t>
    class iota_view : public view_interface<iota_view<W, Bound>> {
      //...
    };
    

    It is already constrained W must be weakly_incrementable. And since iota_view​::​iterator::operator*() returns the same type as W, you can ignore this warning.