"a[++i] = i" vs "a[i] = ++i"
Charles Hixson
charleshixsn at earthlink.net
Mon Dec 23 16:12:05 PST 2013
On 12/23/2013 12:39 PM, David Held wrote:
> On 12/21/2013 6:21 AM, bearophile wrote:
>> aldanor:
>>
>>> So should this considered a bug then and be filed?
>>
>> I think so. I have a related EnhancementRequest open, but I have to
>> close it down or modify it...
>>
>> Bye,
>> bearophile
>
> 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?
>
> Dave
>
>
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.
After all, who's going to know which call would modify the value of i?
Of course, it could be protocol that the functions included within the
statement are all called before the local values are calculated, and
that if the same syntax appears twice, the final value of the syntax is
the value used...but that, while reasonable for the compiler, is hell on
people reading the code.
Note that one can make several different plausible arguments as to the
order in which the code should be evaluated. When such is possible, the
code should be an error. And when it relies on rarely used rules, it
should probably be an error. (E.g., if you define that the location
into which values will be stored must be evaluated before the expression
to be stored is evaluated, you have a case that would be well defined,
but it's a "rare event" kind of rule, and unless there's good reason, it
should be an error.)
--
Charles Hixson
More information about the Digitalmars-d-learn
mailing list