Domanda

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.

È stato utile?

Soluzione

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top