Doug Harrison said:
That wouldn't be likely, since true and false convert to 1 and 0,
respectively, when used in arithmetic expressions. A typical approach is
to
normalize non-bool values to true and false when converting to bool, with
true = 1 and false = 0, and then nothing has to be done to go the other
way.
Well, of course, it would be idiotic to implement a compiler that way.
However, AFAIK, there's nothing in the C++ spec that says I can't implement
it that way as long as 'false' ultimately converts to '0' and true converts
to '1' in emitted assembly. It doesn't detract from the point that relying
on specific compiler conversions is bad practice (i.e. expecting '3' to
translate to 'true' - it may work on one compiler but not on another)
Dammit now you're going to make me go down to the basement tomorrow and
drag out my copy of the ISO spec <g>
But let's not get away from the orgininal post:
the function was declared as expecting a 'bool' parameter. If the caller is
casting a '3' and passing that, the caller is doing something wrong. The
function is expecting a bool, which is a defined type in C++, with two valid
values, true, and false, which are COMPILER defined. Not NECESSARILY 3.
The callee shouldn't have to worry about that...any bugs introduced by
passing the value '3' to the function are COMPLETELY the fault of the caller
not adhering to the function specification declared by the library,
regardless of how the compiler converts ints to bool....
In Windows, BOOL is a typedef for int, and TRUE is #defined as 1 and FALSE
as 0. Of course, you need to treat non-zero as true and in particular
never
compare a BOOL against TRUE.
Damn, I coulda sworn BOOL was a DWORD <shrug>. Either way, I have NEVER
compared a BOOL result of a WinAPI function to TRUE, because most of them
have been documented to be
BOOL FunctionName()
....
On success returns non zero, return zero on failure...yadayada GetLastError
yadayada
--
Doug Semler, MCPD
a.a. #705, BAAWA. EAC Guardian of the Horn of the IPU (pbuhh).
The answer is 42; DNRC o-
Gur Hfrarg unf orpbzr fb shyy bs penc gurfr qnlf, abbar rira
erpbtavmrf fvzcyr guvatf yvxr ebg13 nalzber. Fnq, vfa'g vg?