具有局部变量声明的dispatch_apply 无法在C++ 中编译;方法实现
该代码
class XXX
{
vector<Record> getAll()
{
dispatch_apply(3, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(size_t i) {
for (int j = 0; j < ...; ++j)
{ ... }
});
}
}
无法编译,显示“'int XXX::j' 不是 'class' 的静态成员”。 块上的文档说“在块的词法范围内声明的局部变量,其行为与函数中的局部变量完全相同。”该文件具有 .mm 扩展名。我错过了什么吗?
The code
class XXX
{
vector<Record> getAll()
{
dispatch_apply(3, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(size_t i) {
for (int j = 0; j < ...; ++j)
{ ... }
});
}
}
doesn't compile, saying "'int XXX::j' is not a static member of 'class". The doc on blocks says "Local variables declared within the lexical scope of the block, which behave exactly like local variables in a function. " The file has .mm extension. Did I miss something?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的代码是正确的,clang 会编译它。一般来说,clang 的 C++ 块支持比 gcc 的好得多,如果可以的话,您想使用它。如果您需要使用 gcc,有一个解决方法,使用 ::j 来引用 j。然而,这是非法的 C++,clang 会窒息,所以你可能想让它以所涉及的编译器为条件......
Your code is correct, and clang will compile it. In general clang's C++ blocks support is a lot better then gcc's, and you want to use it if you can. If you need to use gcc there is a workaround, use ::j to refer to j. However that is illegal C++ and clang will choke on it, so you might want to make it conditional on what compiler is involved...