假设我有两个表达式在“||”运算符的左/右。如果左侧表达式为true,则右侧运算符将永远不会被调用。例如,在我的下面的代码中,当“getRand”返回true时,我发现“Foo”将永远不会被调用。我在Mac OSX上的XCode上进行了测试,并想知道它是否是C ++的可靠功能,我们可以依赖 - 如果“||”的左侧部分为真,则永远不会调用右侧部分,还是仅适用于特定平台(例如带有XCode的OSX)?以下是我的代码,谢谢。
感谢您的提前帮助,Lin。
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;
}
感谢您的提前帮助,Lin。
bool
这样的原始类型是安全的。 - kolrabi