c ++ if(func1()&& func2()),是否必须在func2之前调用func1?

use*_*719 0 c++ if-statement

在C++中,如果我编写以下if语句.什么是潜在的问题?行为是否定义明确?谢谢.

int i = 0;
// func1 will return the updated value for i.
// func2 will use the new value of i.
if (func1(i) && func2(i)) { ...}
Run Code Online (Sandbox Code Playgroud)

或者可能&&可以更改为||,呼叫仍然是好的吗?

Mik*_*our 6

是的,&&并且||始终首先评估和的第一个操作数.

第二个操作数仅在需要时根据第一个操作数的值进行评估; 这有时被称为短路.因为&&,如果第一个是真的,它只会被调用; 因为||,只有第一个是假的.