Ordering of boolean values

后端 未结 7 1727
醉酒成梦
醉酒成梦 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 17:44

    bool seems to be defined as a (signed) integer type, false being 0, zero being 1. This explains why true > false (1 > 0) is true.

    Also, comparing -1 to an unsigned number makes -1 be cast to unsigned, and on your platform this causes an integer overflow, resulting UINT_MAX (or whichever type bool has been typedeffed to). This now explains why the following expressions were false:

    ((bool)-1) < true i. e. UINT_MAX < 1
    ((bool)-1) < false i. e. UINT_MAX < 0
    true < false i. e. 1 < 0
    

提交回复
热议问题