Quote:Original post by ToohrVykbool* ptr = (bool*) &x *ptr = <value>;Quote:Original post by Way Walker
But if x is a const bool, then any program that modifies it is malformed.
Oops, I overlooked the const. The compiler can make this optimization safely.
This is C(++), after all -- we can do whatever the hell we want.
If the compiler doesn't correctly perform the optimization, you could try copying the value of the flag to a local variable, which will demonstrate to the compiler that no non-local modifications can be made. I'd only do that after reading the assembly listing and seeing that the compiler wasn't doing the optimization, though.
On the other hand, if you assume a desktop processor, you've got a branch predictor to save your ass here. Even if the compiler doesn't optimize, that compare will be free every single time after the first run through the loop.