Re: [問題] if ( a && b ) 一定會執行到b嗎?

看板C_and_CPP作者 (我要加入劍道社!)時間12年前 (2011/10/09 13:55), 編輯推噓6(607)
留言13則, 7人參與, 最新討論串2/4 (看更多)
short-circuit 是有規定的標準行為 C standard 6.5.13p4 Unlike the bitwise binary & operator, the && operator guarantees left-to-right evaluation; there is a sequence point after the evaluation of the first operand. If the first operand compares equal to 0, the second operand is not evaluated. C++ standard 5.14 Unlike &, && guarantees left-to-right evaluation: the second operand is not evaluated if the first operand is false. 所以如果你的寫法出現 bug 那有兩種可能 1. 你的 compiler 有 bug 2. 你的 code 其它地方有 bug -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 118.168.65.197

10/09 13:57, , 1F
犀利!
10/09 13:57, 1F

10/09 15:02, , 2F
推一下~
10/09 15:02, 2F

10/09 16:44, , 3F
10/09 16:44, 3F

10/09 20:40, , 4F
0_O
10/09 20:40, 4F

10/09 20:40, , 5F
那看來dev真的...XD
10/09 20:40, 5F

10/09 22:09, , 6F
No, gcc 3.4 有提供short circuit這我很確定
10/09 22:09, 6F

10/09 22:09, , 7F
這算是滿基本的功能
10/09 22:09, 7F

10/09 23:07, , 8F
小弟我記得至少在VC2005/2008這應該也都是ok的....
10/09 23:07, 8F

10/09 23:28, , 9F
這邊不包含一種情況,&&是用到user的自訂operator
10/09 23:28, 9F

10/09 23:29, , 10F
如果是user自己寫的 operator&&,會失去這種性質
10/09 23:29, 10F

10/09 23:30, , 11F
它會一定要等兩端參數都到齊
10/09 23:30, 11F

10/09 23:43, , 12F
唔 我的話 &&是內建的
10/09 23:43, 12F

10/11 02:59, , 13F
(∩_∩)這個我要推!
10/11 02:59, 13F
文章代碼(AID): #1EaJTZ3O (C_and_CPP)
文章代碼(AID): #1EaJTZ3O (C_and_CPP)