C++ short-circuiting of booleans

前端 未结 8 1972
生来不讨喜
生来不讨喜 2020-11-28 16:05

I\'m new to c++ and am curious how the compiler handles lazy evaluation of booleans. For example,

if(A == 1 || B == 2){...}

If A does equal

相关标签:
8条回答
  • 2020-11-28 16:34

    The B==2 part is not evaluated.

    Be careful! Don't put something like ++B==2 over there!

    0 讨论(0)
  • 2020-11-28 16:37

    C++ applies short circuiting to Boolean expression evaluation so, the B == 2 is never evaluated and the compiler may even omit it entirely.

    0 讨论(0)
提交回复
热议问题