Why is size_t unsigned?

Maxim Fomin maxim at maxim-fomin.ru
Mon Jul 22 08:39:10 PDT 2013


On Monday, 22 July 2013 at 15:04:25 UTC, monarch_dodra wrote:
> On Monday, 22 July 2013 at 12:51:31 UTC, Andrej Mitrovic wrote:
>> On 7/22/13, JS <js.mdnq at gmail.com> wrote:
>>> foreach doesn't allow you to modify the index to skip over
>>> elements.
>>
>> It does:
>>
>> -----
>> import std.stdio;
>>
>> void main()
>> {
>>    int[] x = [1, 2, 3, 4, 5];
>>    foreach (ref i; 0 .. 5)
>>    {
>>        writeln(x[i]);
>>        ++i;
>>    }
>> }
>> -----
>>
>> Writes:
>> 1
>> 3
>> 5
>
> 99% sure that's unspecified behavior. I wouldn't rely on 
> anything like that.

Of course it is specified behavior.

ForeachStatement:
     Foreach (ForeachTypeList ; Aggregate) NoScopeNonEmptyStatement

Foreach:
     foreach
     foreach_reverse

ForeachTypeList:
     ForeachType
     ForeachType , ForeachTypeList

ForeachType:
     refopt BasicType Declarator
     refopt Identifier

Aggregate:
     Expression

This is an example of unspecified behavior:

import std.stdio;

void main()
{
     int[] x = [1, 2, 3, 4, 5];
     foreach (ref i; 0 .. 5)
     {
         __limit1631--;
         writeln(x[i]);
     }
}


More information about the Digitalmars-d-learn mailing list