"a[++i] = i" vs "a[i] = ++i"

David Held dmd at wyntrmute.com
Mon Dec 23 23:08:27 PST 2013


On 12/23/2013 4:12 PM, Charles Hixson wrote:
> On 12/23/2013 12:39 PM, David Held wrote:
>>[...]
>> int mightUpdate(int& x)
>> {
>>     ...
>>     return x;
>> }
>>
>> {
>>     ...
>>     a[mightUpdate(i)] = mightUpdate(i);
>>     ...
>> }
>>
>> Is this also a bug?  How would the compiler know whether to emit a
>> diagnostic for this case?
>>
> Yes, that should be an error.  And the compiler could notice that the
> function parameter values are allowed to be modified, and forbid such
> use.  As it should.
> [...]

Ok, let's make it more interesting...

void undefinedBehavior(int[] a, int b)
{
     int x = f(b);
     int y = g(b);
     a[mightUpdate(a[x])] = mightUpdate(a[y]);
}

Now, depending on b, f(), and g(), you might have the same as above, or 
you might not.  Should this be illegal, or allowed?

Dave



More information about the Digitalmars-d-learn mailing list