[cfe-dev] failed gcc nested function
Jordan Rose
jordan_rose at apple.com
Tue Aug 21 09:44:05 PDT 2012
On Aug 21, 2012, at 7:42 , Marshall Clow <mclow.lists at gmail.com> wrote:
> On Aug 21, 2012, at 7:14 AM, Shang Yu <yusunn at gmail.com> wrote:
>
>> Hi dear all,
>> The following code will fail in clang:
>>
>> void f (int (*p) (void));
>> void g (int i)
>> {
>> int nestedfunc (void) { return i; }
>> f (nestedfunc);
>> }
>>
>> compile with clang -S test.c got following message
>> test.c:4:24: error: expected ';' at end of declaration
>> int nestedfunc (void) { return i; }
>> ^
>> ;
>> 1 error generated.
>> but gcc -S test.c will be OK.
>
> Clang does not support nested functions - that's a gcc language extension.
>
> See <http://clang.llvm.org/docs/UsersManual.html#c_unsupp_gcc>, which says:
>
> clang does not support nested functions; this is a complex feature which is infrequently used, so it is unlikely to be implemented anytime soon.
>
FWIW, you can use C++11 lambdas or the Blocks extension to do the same thing in a safer way. (Both lambdas and blocks can be used after the enclosing function returns.)
Jordan
More information about the cfe-dev
mailing list