|
-
Oct 21st, 2005, 05:04 PM
#1
Thread Starter
transcendental analytic
side effects
I'm trying to get this to work:
int k=1;
assert(k^(k=0));
but it seems like the VC2003 compiler is assigning k first no matter which side i put it. When I do this however:
int k=1,j=0;
assert((j=k)^(k=0));
it evidently assigns k after evaluating the left side. Is this standard? Is there any way around it without using another variable?
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Oct 22nd, 2005, 07:11 AM
#2
Re: side effects
There is no standard covering any of this. The variable values are only defined at sequence points, of which there are none within the statements you care about. The behaviour of your code is compiler-dependent in both cases. VC++ just happens to evaluate the inner parts from left to right - a different compiler might choose the right side first. Even VC++ might choose the right side first, if the optimizer thinks that's a good idea.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Oct 22nd, 2005, 01:37 PM
#3
Fanatic Member
Re: side effects
Expressions within parentheses are always evaluated first, before the surrounding expression is evaluated. In case of assignments, they're executed during evaluation.
It would be an error if the compiler decides to do so otherwise (because of optimization or for another reason).
-
Oct 22nd, 2005, 03:35 PM
#4
Re: side effects
This is incorrect. The implementation (I'll drop into C++ standard lingo for now) is required to evaluate the (x=0) before the ^, but it is not required to do so before the x; nor is it required to do so afterwards. The implementation could, for instance, evaluate x, fetching its value into a CPU register, then evaluate (x=0), fetching the result into another register, but not writing the value back yet, then executing the ^. If x is declared volatile, the implementation must write the value of x back at this point, as the call of a function constitutes a sequence point. If it is not, the implementation can, in theory, delay until the next time x is actually used after the sequence point.
The second example follows the same principle, but it's more obvious.
In essence, you are violating clause 5.1 of the standard, which, in the final draft, says:
Between the previous and next sequence point a scalar object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be accessed only to determine the value to be stored.
Emphasis mine. You are accessing the prior value (but is it?) for purposes other than determining the value to be stored. Your program is ill-formed.
This is based on my understanding of the standard, which is far from trivial. If you want an answer you can really trust, you should ask the question on comp.lang.c++.moderated and watch the ensuing flame war
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Oct 23rd, 2005, 12:03 AM
#5
Frenzied Member
Re: side effects
I assumed the same as riis. In my opinion, parenthesied expressions should always be evaluated first.
By the way, kedaman, thanks for turning me on to Opera! It is so much better than Firefox!!
-
Oct 23rd, 2005, 01:27 AM
#6
Fanatic Member
Re: side effects
 Originally Posted by CornedBee
This is incorrect. The implementation (I'll drop into C++ standard lingo for now) is required to evaluate the (x=0) before the ^, but it is not required to do so before the x; nor is it required to do so afterwards. <snip/>
Ah, I see, the ^ isn't relevant here. Any binary operator could've caused this "effect".
It's pretty much the same as that the evaluation order of function parameters doesn't have a fixed order, but can also be compiler-optimized, right?
-
Oct 23rd, 2005, 06:10 AM
#7
Re: side effects
 Originally Posted by riis
Ah, I see, the ^ isn't relevant here. Any binary operator could've caused this "effect".
Not quite. The , && || operators create sequence points.
It's pretty much the same as that the evaluation order of function parameters doesn't have a fixed order, but can also be compiler-optimized, right?
Right.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Oct 25th, 2005, 12:50 PM
#8
Thread Starter
transcendental analytic
Re: side effects
Thanks everyone (esp. CB)
It seems like I have to do it the ugly way, that is by declaring another variable 
By the way, kedaman, thanks for turning me on to Opera! It is so much better than Firefox!!
So true You're welcome
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|