Ordering of boolean values

后端 未结 7 1729
醉酒成梦
醉酒成梦 2021-02-18 17:02

Under C++ or from C99, how is the less-than operator < defined for boolean values?

Alternatively, explain the behaviour of

相关标签:
7条回答
  • 2021-02-18 18:02

    This makes perfect sense. The integral type => bool conversion is effectively b = i != 0. In order to do the < comparison it promotes the bool to int by the rule false=>0 and true=>1. In your first case -1 will equate to true, and both will promote to 1 so it's false. Obviously 1 is never less than 0 for the second and third cases, while 0 < 1 in the last case.

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