NoNo for Associativity of Assignments?

Joel C. Salomon JoelCSalomon at Gmail.com
Sun Feb 25 12:31:54 PST 2007


Manfred Nowak wrote:
> Joel C. Salomon wrote
>> the meaning of chained assignments is unambiguous to compiler and
>> programmer alike. 
> 
> Are you sure?
> 
>   int a=1, b=2, c=3;
>   a = b = c;
> 
> With associativity this can virtually be rewritten as both
>   ( a = b ) = c;
> or
>   a = ( b = c);
> 
> After "( a = b) = c;" a == 2, b == 3, c == 3.
> After "a = ( b = c);" a == 3, b == 3, c == 3.
> 
> Of course one can _declare_ this to be unambigouous.

In C-like languages, assignment is right-associative; a = b = c /always/ 
is parsed as a = (b = c).

Also consider if (a = b) = c is even valid — does the expression (a = b) 
yield an lvalue?

--Joel



More information about the Digitalmars-d mailing list