作者sbrhsieh (sbr)
看板C_and_CPP
标题Re: [问题] 请问优先「计算」的问题
时间Wed Apr 1 15:48:23 2009
※ 引述《loteslogin (张三立)》之铭言:
: 以下面这个算式来说:
: return ((a+=5) > 9) ? a+2 : a+4;
: 以「结合」的优先顺序当然是 a+= 还有 + 会优先结合,
: 但是以「计算」的优先顺序来说,
: 不知道标准 C/C++ 是否有规定关於这个式子在计算上的优先顺序呢?
C++ standard 里有规定 conditional operator 第一个 exporession 的
side effect(不含 temporary object destruction)会发生在第二与第三个
expression 求值之前。第二与第三个 expression 只有其中一个会被求值(depends
on first expression)。
请见
ISO/IEC 14882, 2003
5.16 Conditional operator
一节。
节录一小段於此:(如果此举是违法的,请告知在下删除,谢谢)
Conditional expressions group right-to-left. The first expression is
implicitly converted to bool (clause 4). It is evaluated and if it is true,
the result of the conditional expression is the value of the second
expression, otherwise that of the third expression. All side effects of the
first expression except for destruction of temporaries (12.2) happen before
the second or third expression is evaluated. Only one of the second and
third expressions is evaluated.
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 218.173.134.87
※ 编辑: sbrhsieh 来自: 218.173.134.87 (04/01 16:25)
1F:推 MOONRAKER:赞 04/01 18:29
2F:推 hylkevin:推一下 04/01 19:16
3F:推 yoco315:好强大 XD 04/01 20:30
4F:推 loteslogin:感谢!实在太强了!请问资料哪里找的? 04/02 15:48