[Bug 24] Arithmetic operators are allowed on boolean expressions

Sean Kelly sean at f4.ca
Thu Mar 9 22:36:20 PST 2006


A few nits.

d-bugmail at puremagic.com wrote:
> 
> ------- Comment #5 from walter at digitalmars.com  2006-03-10 00:21 -------
> First, note that the bool operands undergo integral promotion rules, so that
> (true op true) is evaluated as (cast(int)true op cast(int)true), with a result
> type of int. The integral promotion for bools does not apply if op is & | ^
> 
> void main()
> {
>     bool x1 = true + true; // fails because true+true => 2, and 2 cannot be
> implicitly converted to bool
> 
>     bool x2 = true - true; // accepted because true-true=>0, and 0 can be
> implicitly converted to bool
> 
>     bool x3 = true * true; // accepted because true*true => 1, and 1 can be
> implicitly converted to bool
> 
>     bool x4 = true / true; // accepted because true/true => 1, and 1 can be
> implicitly converted to bool
> 
>     bool x5; x5 += true;  // fails because x5+true is int, and int cannot be
> implicitly converted to bool unless it is 0 or 1

bool defaults to false/0, so x5 + true should equal 1, which should not 
fail.

>     bool x6; x6 -= true; // fails because x6-true is int, and int cannot be
> implicitly converted to bool unless it is 0 or 1
> 
>     bool x7; x7 *= true; // fails because x7*true is int, and int cannot be
> implicitly converted to bool unless it is 0 or 1

Again no fail.  0 * 1 == 0.

>     bool x8; x8 /= true; // fails because x8/true is int, and int cannot be
> implicitly converted to bool unless it is 0 or 1
> }

No fail here either, as 0 / 1 == 0.


Sean



More information about the Digitalmars-d-bugs mailing list