C ++ function returning bool failed
2 answers
This is called short circuit evaluation .
In your example some_bool
is false, so the operator some_bool && foo()
will always be false. Therefore, you never need to evaluate foo()
.
Please note that this is standard C / C ++ and is not compiler dependent , as this may lead to incorrect code that you find.
Best way to write code:
bool some_bool = false;
bool foo_result = foo();
some_bool = some_bool && foo_result;
+5
source to share