Suppose I have two expressions left/right of ||
operator. I find if left expression is true, the right operator will never be called. For example, in my below code, when getRand
returns true, I found Foo
will never be called. I tested on XCode on Mac OSX, and wondering if it is a reliable feature of C++ we could rely on -- if left part of || is true, right part will never be called, or it is a special feature just for specific platform (e.g. OSX with XCode)? Post my code below, thanks.
bool Foo()
{
std::cout << "I am called!\n";
return false;
}
bool getRand()
{
int random_variable = std::rand();
std::cout << random_variable << '\n';
return random_variable % 2 == 1;
}
int main(int argc, const char * argv[]) {
if (getRand() || Foo())
{
std::cout<<"Hello World \n";
}
return 0;
}
thanks in advance, Lin