Why iota_view allows different integer template arguments?

Viewed 112

I have the following broken code, from what I can tell problem here is that iota(0, n) returns me a iota_view<int,int64> and then obviously int can never be int64 that is greater than INT_MAX. Easy fix is to just use iota(0LL, n), but that seems error prone.

int main() {
  const int64_t n = 16LL*1024*1024*1024;
  auto ints = std::ranges::views::iota(0, n) |
              std::views::transform([](int64_t x) { return x * 10; });
  for (int64_t lookup : {49LL, 50LL, 51LL}) {
    const auto it = std::ranges::lower_bound(ints, lookup);
    if (it != ints.end()) {
      std::cout << *it << std::endl;
      std::cout << *it.base() << std::endl;
    }
  }
}

My best guess is that iota_view wants to work with "weird" second type, like some kind of +INF type, so that is why it needs 2 types, and nobody thought of forcing the first argument to match the second one if they are both ints.

0 Answers
Related