digitalmars.D - ? valid opAssign
- Thomas Kuehne <thomas-dloop kuehne.thisisspam.cn> Nov 16 2004
- Daniel Keep <daniel.keep dummy.gmail.com> Nov 16 2004
- Regan Heath <regan netwin.co.nz> Nov 16 2004
- Daniel Keep <daniel.keep dummy.gmail.com> Nov 17 2004
Is the code below valid?
# int main(){
# int a=2;
# int b;
# return b=b=a;
# }
The reason I ask is that this code passes the compiler,
but I can't find and documentation on it's behaviour.
Thomas
Nov 16 2004
Ok, I could be wrong, BUT, I think that it's valid. The trick here is that (unless Walter changed it from C/C++), assignment can be used in an expression. Specifically, (a=b) first assigns the value of b to a, then returns a. So `return b=b=a;' is assigning a to b, b to itself, then returning the value of b. Maybe :P -- Daniel Thomas Kuehne wrote:Is the code below valid? # int main(){ # int a=2; # int b; # return b=b=a; # } The reason I ask is that this code passes the compiler, but I can't find and documentation on it's behaviour. Thomas
Nov 16 2004
On Tue, 16 Nov 2004 19:11:30 +1100, Daniel Keep <daniel.keep dummy.gmail.com> wrote:Ok, I could be wrong, BUT, I think that it's valid. The trick here is that (unless Walter changed it from C/C++), assignment can be used in an expression. Specifically, (a=b) first assigns the value of b to a, then returns a. So `return b=b=a;' is assigning a to b, b to itself, then returning the value of b.
Or, is the order reversed, i.e. assigns b to b, then a to b. Of course in both cases the result is the same, b=a. The real question is, is the order defined? If so, what is it? ReganThomas Kuehne wrote:Is the code below valid? # int main(){ # int a=2; # int b; # return b=b=a; # } The reason I ask is that this code passes the compiler, but I can't find and documentation on it's behaviour. Thomas
-- Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Nov 16 2004
Hmm... my intuition tells me it's right to left. Assignment *should* be right associative--it computes the right hand side first (thus, performing all the assignments right to left). But then again, IANALL Regan Heath wrote:On Tue, 16 Nov 2004 19:11:30 +1100, Daniel Keep <daniel.keep dummy.gmail.com> wrote:Ok, I could be wrong, BUT, I think that it's valid. The trick here is that (unless Walter changed it from C/C++), assignment can be used in an expression. Specifically, (a=b) first assigns the value of b to a, then returns a. So `return b=b=a;' is assigning a to b, b to itself, then returning the value of b.
Or, is the order reversed, i.e. assigns b to b, then a to b. Of course in both cases the result is the same, b=a. The real question is, is the order defined? If so, what is it? ReganThomas Kuehne wrote:Is the code below valid? # int main(){ # int a=2; # int b; # return b=b=a; # } The reason I ask is that this code passes the compiler, but I can't find and documentation on it's behaviour. Thomas
Nov 17 2004








Daniel Keep <daniel.keep dummy.gmail.com>