DMD 0.148 release
Thomas Kuehne
thomas-dloop at kuehne.cn
Sun Feb 26 14:03:20 PST 2006
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Regan Heath schrieb am 2006-02-26:
> On Sun, 26 Feb 2006 18:46:10 +0000 (UTC), Thomas Kuehne
><thomas-dloop at kuehne.cn> wrote:
>> Charles schrieb am 2006-02-26:
>>>
>>> > Can't we simply treat zero as false and non-zero as true as we C
>>> > programmers always do?
>>>
>>> I agree, I still don't get what the 'true bool' fuss is about .
>>
>> if(b==1) { ... }
>>
>> instead of
>>
>> if(b!=0) { ... }
>>
>> can be found in quite a lot of C code ...
>
> So?
>
> Assuming:
>
> - 'b' is a bool
> - a bool can only have 2 values, 'true' and 'false'
> - when you convert/promote a bool to an int you get: 'true'->'1',
> 'false'->'0'
> - when you convert/promote an int to bool you get: '0'->false,
> '!0'->'true'
>
> Then:
>
> if(b==1) { ... }
>
> results in 'b' being converted to int, giving it the value 1, and the
> comparrison working correctly.
>
> Right?
# import std.stdio;
#
# int main(){
# int i = 3;
# bool a = true;
# bool b = *(cast(bool*)cast(void*) &i);
#
# writefln("a: %s", a);
# writefln("b: %s", b);
# writefln("int: %s", cast(int)b); // line 9
# writefln("a+b: %s", a+b); // line 10
#
# return 0;
# }
I know, nobody writes code like that ...
surprise:
true -> 3 // line 9
true + true -> 4 // line 10
> - when you convert/promote a bool to an int you get: 'true'->'1',
> 'false'->'0'
The problem is: that assumtion isn't easily enforcable if bool is
treated as an integer type.
Thomas
-----BEGIN PGP SIGNATURE-----
iD8DBQFEAjK+3w+/yD4P9tIRAoFmAJ4lNdg1i5/SbYNKvN+21jiF+L4wiACgusY4
JZugXrusKOTK629hFhxmR9A=
=oxIq
-----END PGP SIGNATURE-----
More information about the Digitalmars-d-announce
mailing list