C++ function returning bool not executed -
this question has answer here:
- short circuit evaluation , side effects 3 answers
given c++ function foo
:
bool foo();
and following lines of code
bool some_bool = false; some_bool = some_bool , foo();
i observed foo()
not called although might have side-effects. name of behavior , compiler-dependent?
this called short-circuit evaluation.
in example, some_bool
false , statement some_bool && foo()
going false. there never need evaluate foo()
.
note standard c/c++ , not compiler dependent can lead unperformed code you've discovered.
a better way of writing code is:
bool some_bool = false; bool foo_result = foo(); some_bool = some_bool && foo_result;
Comments
Post a Comment