Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can a for range loop infer a plain array size

Consider this snippet:

#include <iostream>

int main() {
  int s[6] {0, 1, 2, 3, 4, 5};
  for ( auto && i: s ) {
    std::cout << " " << i << std::endl;
  }
}

This compiles and runs happily both under g++ and clang++.
It is taken for granted in many posts(here and here, for example), but it is unclear to me how the compiler can correctly infer the size of the array in the for range for a type without iterator.
Can anyone answer or add a link to the reference?

like image 949
fedino Avatar asked Jan 05 '23 19:01

fedino


1 Answers

According to the working draft [6.5.4/1]:

The range-based for statement

for ( for-range-declaration : for-range-initializer ) statement

is equivalent to

{
  auto &&__range = for-range-initializer ;
  auto __begin = begin-expr ;
  auto __end = end-expr ;
  for ( ; __begin != __end; ++__begin ) {
      for-range-declaration = *__begin;
      statement
  }
}

where

[...]

begin-expr and end-expr are determined as follows:

  • if the for-range-initializer is an expression of array type R, begin-expr and end-expr are __range and __range + __bound, respectively, where __bound is the array bound. If R is an array of unknown size or an array of incomplete type, the program is ill-formed;

[...]

So it works mostly as @VladFromMoscow has already mentioned in his answer.

like image 111
skypjack Avatar answered Jan 16 '23 14:01

skypjack