Is the operation “false < true” well defined?

后端 未结 4 729
孤城傲影
孤城傲影 2020-12-24 11:40

Does the C++ specification define:

  1. the existence of the \'less than\' operator for boolean parameters, and if so,
  2. the result of the 4 parameter permut
4条回答
  •  -上瘾入骨i
    2020-12-24 11:46

    According to the C++ Standard (5.9 Relational operators)

    2 The usual arithmetic conversions are performed on operands of arithmetic or enumeration type.

    and

    1...The type of the result is bool.

    and (3.9.1 Fundamental types)

    6 Values of type bool are either true or false.49 [ Note: There are no signed, unsigned, short, or long bool types or values. —end note ] Values of type bool participate in integral promotions (4.5).

    and (4.5 Integral promotions)

    6 A prvalue of type bool can be converted to a prvalue of type int, with false becoming zero and true becoming one.

    So in all your examples true is converted to int 1 and false is converted to int 0

    These expressions

    false < false
    false < true
    true < false
    true < true
    

    are entirely equivalent to

    0 < 0
    0 < 1
    1 < 0
    1 < 1
    

提交回复
热议问题