是的,对于C和C++标准中的操作员||
和&&
都需要短路和判断顺序.
C++标准说(C标准中应该有一个等价的子句):
1.9.18
In the evaluation of the following expressions
a && b
a || b
a ? b : c
a , b
using the built-in meaning of the operators in these expressions, 100 (12).
在C++中有一个额外的trap :短路确实NOT适用于重载操作符||
和&&
的类型.
Footnote 12: The operators indicated in this paragraph are the built-in operators, as described in clause 5. When one of these operators is overloaded (clause 13) in a valid context, thus designating a user-defined operator function, the expression designates a function invocation, and the operands form an argument list, without an implied sequence point between them.
通常不建议在C++中重载这些运算符,除非您有非常特定的要求.您可以这样做,但它可能会 destruct 其他人代码中的预期行为,尤其是如果这些运算符是通过使用重载这些运算符的类型实例化模板间接使用的.