foreach counter now must be size_t ?

Nicholas Wilson iamthewilsonator at hotmail.com
Fri Feb 8 08:06:39 UTC 2019


On Thursday, 7 February 2019 at 23:02:14 UTC, Rubn wrote:
> I'll take that to mean you don't think this is a bug:
>
> @safe void foo(int[] a) {
>     for(int i = 0; i < a.length; ++i ) {
>        writeln( i );
>     }
> }
>
> Fucking classic.

Its not a bug. In the case that a.length > int.max, the loop will 
not terminate and only print indices. If the loop was

      for(int i = 0; i < a.length; ++i ) {
         writeln( a[i] );
      }

it would still be @safe, but the program would crash because the 
index would (eventually) be OOB. If a.length > uint.max and the 
loop was

      for(uint i = 0; i < a.length; ++i ) {
         writeln( a[i] );
      }

then the loop fail to terminate, and it would still be @safe. All 
the above problems are avoided using size_t as the index.


More information about the Digitalmars-d mailing list