“typedef void (^Something)()”是什么意思

What does “typedef void (^Something)()” mean

我正在尝试编译 stk。在配置期间我收到错误

System/Library/Frameworks/CoreAudio.framework/Headers/AudioHardware.h:162:2: error: expected identifier or '(' before '^' token (^AudioObjectPropertyListenerBlock)(

当我看到代码时,我在 here 的第 162 行的函数指针声明中看到 ^。我知道我们可以 *^ 是什么意思?

代码片段:

#if defined(__BLOCKS__)
typedef void
(^AudioObjectPropertyListenerBlock)(    UInt32                              inNumberAddresses,
                                        const AudioObjectPropertyAddress    inAddresses[]);

正如这里其他回答者所说,它可能在 C++/CLI 中。

而且,如果您使用的是 macOS(就像您在一条评论中暗示的那样),这是一个 Objective-C 块.

它的语法非常非常奇怪。

块就像一个C++闭包和Java匿名内部classes,它可以捕获变量。

__block int insider = 0;

void(^block)() = ^{
    // print insider here using your favourite method,  printf for example
};

这是一个完整的NSObject(基础Objective-Cclass),但是是可调用的,这不仅仅是一个函数指针。

参考这个 Apple 文档:https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/WorkingwithBlocks/WorkingwithBlocks.html

现在,我们进入重要的问题,我想 运行 这个 Linux,怎么样???

LLVM 支持块语法,但您应该参考这个 Whosebug 问题了解更多信息:Clang block in Linux?

因此,您应该在 LLVM 编译器中编译代码,并使用 -fblocks 和 -lBlocksRuntime。

不要忘记安装那些 Linux 软件包:

llvm 铛 libblocks运行时间开发

如果您已经在使用 macOS,只需使用 -fblocks。