Re: More 'bool' weirdness
Re: More 'bool' weirdness
- Subject: Re: More 'bool' weirdness
- From: Jeffrey Walton <email@hidden>
- Date: Fri, 14 Jun 2013 21:32:05 -0400
On Fri, Jun 14, 2013 at 6:58 PM, Jens Alfke <email@hidden> wrote:
> OK, now I’ve hit a situation where the C99 ‘bool’ type is behaving weirdly. The following snippets all compile without errors or warnings:
>
> bool foo = @“huh”;
>
> bool foo = [NSMutableArray array];
>
> bool x = &x;
>
> static bool test(void) { return &test; }
-Wconversion should do the trick. It will also warn you about a lot of
other details many don't pay attention to until their appearance on
Bugtraq or Full Disclosure. Signed/unsigned conversions are always
neat because -1 > 1 after promotion.
> In general it seems to be legal to assign any pointer or integer value to a LHS of type bool.
You get pointer-to-bool conversion and bool-to-int conversion for free.
> But try this with BOOL or with any integer type, and you get an error. (If you try to assign a float to a bool, you do get an error about an implicit conversion.)
You don't get pointer-to-int (assuming BOOL is typedef'd as an int).
> The only explanation I can think of is that when assigning to bool the compiler adds the same implicit “!= 0” test that it does if you put a non-bool expression in an if() or while() test … but that’s nuts. It’s totally not the right thing to do here; it’s more likely to just hide bugs (as it was doing in my code.)
>
Well, C++ tries to be backwards compatible with C, so it has to accept
things like (and the reason for pointer-to-bool):
void* p = ...;
if(p)
...
Also, rather than “!= 0”, you can use "!!". Its often a little cleaner
in the code:
BOOL B = YES;
bool b = !!B;
Jeff
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden