c++std-ranges

Failing to create a std::ranges::iota_view with value and bound


I tried to create a std::ranges::iota_view with a value and a bound:

#include <ranges>
#include <vector>

int main() {
  std::vector v{1,2,3};
  auto indices = std::ranges::views::iota(0, v.size());
}

link to program on godbolt

But clang 11.0.0 fails to compile with the error message:

no viable constructor or deduction guide for deduction of template arguments of 'iota_view'

What am I doing wrong?


Solution

  • From https://en.cppreference.com/w/cpp/ranges/iota_view

    Note that the [deduction] guide protects itself against signed/unsigned mismatch bugs, like views::iota(0, v.size()), where 0 is a (signed) int and v.size() is an (unsigned) std::size_t.

    This will work:

    auto indices = std::ranges::views::iota(0u, v.size());
                                             ^ unsigned zero literal