[cairo] Adding a boolean type

Owen Taylor otaylor at redhat.com
Mon Jan 31 23:03:34 PST 2005


On Mon, 2005-01-31 at 22:24 -0800, Ned Konz wrote:

> (1) Standard C has had the _Bool (and bool if you include <stdbool.h>) for 
> some time now (5 years or so). Ideally we should stay compatible with the 
> standard, and allow using the standard _Bool type if it exists.

_Bool has different behaviors... it converts to 0/1 on cast. As a
language extension, not a header file extension, using it #ifdef'ed
strikes me as inviting obscure bugs.

> (2) Using char for a bool_t keeps you from declaring groups of bool_t in 
> bitfields, at least if you want to be compatible with Standard C:
> 
> struct {
>    char b1: 1;
>  char b2: 1;
>  char b3: 1; } // not legal Standard C
> 
> vs.
> 
> struct {
>  unsigned int b1: 1; // OK

Well, char is really no more non-functioning there than using 'int'.
For the GTK+ stack, we've just accepted declaring bitfields as "unsigned
int" instead of gboolean. I suppose it is an argument for using
'unsigned int' 

> etc.
> 
> (3) you may get diagnostics from your compiler or lint, since the default C 
> promotion to int will result in initializing from, assigning, and comparing 
> dissimilar types:
> 
> cairo_bool_t myBool = CAIRO_FALSE; // initializing char from int

Any compiler that warns on that isn't worth using... it's also going
to warn on "char c = 'a'"....

> if (myBool == (3 == 4)) { ... }  // comparing char to int

Similarly,  if (c == 'a') ... I'm a little skeptical that you'll
get warnings from any compiler from using char as boolean. A lot
of libraries do it, e.g. GL. (Actually, more commonly unsigned char.) 

> I'd suggest using an unsigned int or int as the bool_t type, for these 
> reasons.

I don't really have a strong preference on [unsigned] char vs. 
[unsigned] int. There are weak reasons one way or the other.

Regards,
					Owen

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part
Url : http://lists.freedesktop.org/archives/cairo/attachments/20050201/0a58ceed/attachment.pgp


More information about the cairo mailing list