Question

In C++, If I have a boolean that is already true, and I perform an |= on it, it is guaranteed to still execute whatever is to the right of the operator?

For example:

bool myBool = true;
myBool |= someFcnThatReturnsBoolean();

I find that it is executing that function, but I need to know if it's explicitly guaranteed by a standard that it will be guaranteed to execute. I'd hate to run into an issue where it stops executing based on some optimization, or on certain compilers.

Was it helpful?

Solution

bool myBool = true;
myBool |= someFcnThatReturnsBoolean();

would be equivalent to

bool myBool = true;
myBool = myBool | someFcnThatReturnsBoolean();

The bitwise OR (|) will always evaluate both operands, thus your function will always be called.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top