Casting an expression to bool means testing for 0 or !=0 for arithmetic types

Simen kjaeraas simen.kjaras at gmail.com
Sat Jul 31 15:40:34 PDT 2010


Pluto <pluto at planets.not> wrote:

> This part has always bothered me. Could somebody please explain to me the
> rationale behind limiting functions to one usable error code?

Well, traditionally it was done because testing for 0/non-0 is a simple
and fast operation. Also, boolean logic can be thought of as simple maths,
as AND is multiplication and OR is addition. This only makes sense if
false == 0.

As for having more error codes, why would you use an int for it? Would it
not be more logical to use an enum, and to explicitly test for the value
upon return? Why not use exceptions?

If the problem is that enums are non-extensible, and exceptions are too
heavy, many libraries use the convention that 0 means success, and
anything else is an error code.

-- 
Simen


More information about the Digitalmars-d-learn mailing list