Passing block parameter that doesn't match signature

前端 未结 3 1514
南笙
南笙 2020-12-31 18:47

I\'m working with a block-based API and stumbled across a scenario where I was passing in a block parameter that had a signature that didn\'t match the typedef\'d parameter

3条回答
  •  我在风中等你
    2020-12-31 18:58

    From the Clang Blocks specification:

    Variadic ... arguments are supported. [variadic.c] A Block that takes no arguments must specify void in the argument list [voidarg.c]. An empty parameter list does not represent, as K&R provide, an unspecified argument list. Note: both gcc and clang support K&R style as a convenience.

    Basically, this is an ancient quirk of C syntax. In olden times, C function declaration syntax was rather different, and empty parentheses indicated that the function could be passed any number of arguments. For backwards compatibility, compilers have generally allowed the old-style function declaration syntax. And for some reason, Apple decided to simultaneously reject this syntax in the block standard while actually allowing it to be used with blocks in both GCC and Clang.

    So, long story short: To declare that a block takes no arguments, you need to explicitly type it as void(^MyBlock)(void)

提交回复
热议问题