← Back to team overview

unity-dev team mailing list archive

[Ayatana-dev] [C++] gboolean and bool

 

Hi All,

 

I've feel compelled to write this email after a discussion on a code review.

 

bool is the native C++ type, and gboolean is the glib boolean type:

typedef gint gboolean;

and

typedef int gint;

 

This gives us some new interesting ways to shoot ourselves in the foot.

 

#define FALSE (0)

#define TRUE (!FALSE)

 

Since ! is the logical not operator, !0 -> 1 (defined in the standard).

 

The C (and C++) standard say that 0 is false, and anything else is considered

to be true.  If we are checking for equality to TRUE on an integral type, we 
may well have situations where the variable is neither TRUE nor FALSE.  We 
should only ever check for FALSE, or != FALSE.  Checking for TRUE is a world 
of pain for integral types.

bool bool_test = 42;

gboolean g_test = 42;

 

bool_test == true -> true // 42 is converted to true at assignment time

g_test == TRUE -> false

g_test == FALSE -> false

bool(g_test) -> true

 

if statements and assignment to bool will implicitly cast the gboolean to a bool. This uses the standard definition for true and false, and as such, a value like 42 is considered true.

 

If you feel like you really must check for a value, please check for FALSE, or != FALSE. This is the only true safe way to check an integral value for "true".

 

Tim

Attachment: signature.asc
Description: This is a digitally signed message part.