Rectangle 27 1

c++ Is the operation "false true" well defined?


0 < 0
0 < 1
1 < 0
1 < 1
4.5
5.9
boolean-literal:
    false
    true
false < false
false < true
true < false
true < true

@chris I am not super familiar with that section so I would have to think about it but I don't think the answer changes from what I can see.

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

Going back to section 5.9 to see the mechanics of the relational operators further, it says:

Integral and floating types are collectively called arithmetic types.

Nice, that's about as explicit as any answer could possibly be, while still easy to read. A nit: I think you bolded the wrong "type": "The operands shall have arithmetic, enumeration, or pointer type, or type std::nullptr_t." Adding parentheses for clarity gives ((arithmetic, enumeration, or pointer) type) or (type std::nullptr_t).

Otherwise, the integral promotions (4.5) shall be performed on both operands

The operands shall have arithmetic, enumeration, or pointer type, or type std::nullptr_t. The operators < (less than), > (greater than), <= (less than or equal to), and >= (greater than or equal to) all yield false or true. The type of the result is bool

The operations are well defined according to the draft C++ standard.

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

Types bool, char, char16_t, char32_t, wchar_t, and the signed and unsigned integer types are collectively called integral types.

Yeah, the promotion is the first thing to happen either way.

and

and bools are arithematic types from 3.9.1 Fundamental types

and true and false are boolean literals from 2.14.6 Boolean literals:

Note
Rectangle 27 1

c++ Is the operation "false true" well defined?


0 < 0
0 < 1
1 < 0
1 < 1
4.5
5.9
boolean-literal:
    false
    true
false < false
false < true
true < false
true < true

Sign up for our newsletter and get our top new questions delivered to your inbox (see an example).

@chris I am not super familiar with that section so I would have to think about it but I don't think the answer changes from what I can see.

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

Going back to section 5.9 to see the mechanics of the relational operators further, it says:

Integral and floating types are collectively called arithmetic types.

Nice, that's about as explicit as any answer could possibly be, while still easy to read. A nit: I think you bolded the wrong "type": "The operands shall have arithmetic, enumeration, or pointer type, or type std::nullptr_t." Adding parentheses for clarity gives ((arithmetic, enumeration, or pointer) type) or (type std::nullptr_t).

Otherwise, the integral promotions (4.5) shall be performed on both operands

The operands shall have arithmetic, enumeration, or pointer type, or type std::nullptr_t. The operators < (less than), > (greater than), <= (less than or equal to), and >= (greater than or equal to) all yield false or true. The type of the result is bool

The operations are well defined according to the draft C++ standard.

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

Types bool, char, char16_t, char32_t, wchar_t, and the signed and unsigned integer types are collectively called integral types.

Yeah, the promotion is the first thing to happen either way.

and

and bools are arithematic types from 3.9.1 Fundamental types

and true and false are boolean literals from 2.14.6 Boolean literals:

Note
Rectangle 27 1

c++ Is the operation "false true" well defined?


...and relational operators are specified to perform the usual arithmetic conversions (which includes integer promotions) on operands of arithmetic or enumeration type.

@KRyan I can't go back quite as far as K+R, but I dug out my copy of the 1990 ANSI C Standard. Section 6.3.8 says "Each of the operators < (less than), > (greater than), <= (less than or equal to), and >= (greater than or equal to) shall yield 1 if the specified relation is true and 0 if it is false. The result has type int."

@KRyan what, you're not going to take my word for it? :) Before there was a bool type, before there was even C++, the result of a boolean operation was defined as 0 for false and 1 for true. I wouldn't be surprised if you can find it in K+R.

Boolean values are subject to the usual integer promotions, with false defined as 0 and true defined as 1. That makes all the comparisons well defined.

I like that this answer is shorter than Shafik's, but I think the key point that false is defined as 0 and true is defined as 1 in the standard (rather than just by common practice) needs evidence to back it up.

The biggest problem IIRC was that in K&R, enum bool { false = 0, true = 1} was legal but didn't define an operator<.

Note
Rectangle 27 1

c++ Is the operation "false true" well defined?


...and relational operators are specified to perform the usual arithmetic conversions (which includes integer promotions) on operands of arithmetic or enumeration type.

@KRyan I can't go back quite as far as K+R, but I dug out my copy of the 1990 ANSI C Standard. Section 6.3.8 says "Each of the operators < (less than), > (greater than), <= (less than or equal to), and >= (greater than or equal to) shall yield 1 if the specified relation is true and 0 if it is false. The result has type int."

@KRyan what, you're not going to take my word for it? :) Before there was a bool type, before there was even C++, the result of a boolean operation was defined as 0 for false and 1 for true. I wouldn't be surprised if you can find it in K+R.

Boolean values are subject to the usual integer promotions, with false defined as 0 and true defined as 1. That makes all the comparisons well defined.

I like that this answer is shorter than Shafik's, but I think the key point that false is defined as 0 and true is defined as 1 in the standard (rather than just by common practice) needs evidence to back it up.

The biggest problem IIRC was that in K&R, enum bool { false = 0, true = 1} was legal but didn't define an operator<.

Note