开发者

dispatch_apply with local variable declaration doesn't compile in C++ method implementation

The code

class XXX
{
   vector<Record> getAll()
   {
      dispatch_apply(3, dispatch_get_global_q开发者_StackOverflowueue(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?


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...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜