C++ function returning bool not executed -


this question has answer here:

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

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -