Re: Block variable syntax related question
Re: Block variable syntax related question
- Subject: Re: Block variable syntax related question
- From: Bavarious <email@hidden>
- Date: Fri, 09 Sep 2011 22:35:25 -0300
On 9 Sep 2011, at 18:47, Jens Alfke wrote:
> On Sep 9, 2011, at 12:38 PM, Bavarious wrote:
>> In this particular case, the conditional operator promotes the second and third operands to int as specified in the C99 standard. This means that
>>
>> [obj compare:@"foo"] == 0 ? YES : NO
>>
>> is an int expression, not a BOOL one. The block variable has return type BOOL and the block literal has inferred return type int, hence the compiler error.
>
> I think this stems from BOOL being merely a typedef for ‘signed char’, not a real type.
Integer promotions happen with non-typedef types as well. Consider the following:
typedef bool (^Tbool)(id obj);
typedef char (^Tchar)(id obj);
typedef short (^Tshort)(id obj);
Tbool b = ^(id obj) { return obj ? true : false; };
Tchar c = ^(id obj) { return obj ? 'y' : 'n'; };
Tshort s = ^(id obj) { return obj ? (short)0 : (short)1; };
In all three cases the operands of the conditional operator are promoted to int, so those three block literals have int as the inferred return type and the compiler generates return type mismatch errors.
The CERT C Secure Coding Standard document has an interesting section on integer conversions:
https://www.securecoding.cert.org/confluence/display/seccode/INT02-C.+Understand+integer+conversion+rules
-- Bavarious _______________________________________________
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