1

I'm using GCD to speed up some calculations in a few C++ templates I'm working on. I've already done it successfully for several functions, but now I'm trying to get it to work for a member function and I've encountered this weird scoping problem. The code looks something like this:

inline void op::factorOutGaussian(const double *x, const complex *y)  
{  
    op a = *this;  
    NSInteger modes = op::modes;  
    NSInteger modes2 = modes*modes;  
    NSInteger *m = op::m;  

    dispatch_queue_t globalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

    void (^block)(size_t) = ^(size_t i) {
        NSInteger mi = m[i];
        if (mi == 0) {
            for (NSInteger j = 0; j < modes; j++) {
                this->_a1[i] += a._b1[i*modes+j] * x[j];
                for (NSInteger k = 0; k < modes; k++) {
                    this->_a1[i] += a._c1p[i*modes2+j*modes+k] * x[j] * x[k]
                    + a._c1m[i*modes2+j*modes+k] * y[j*modes+k];
                }
            }
        }

    //A bunch more loops like the one above follow.  You get the idea.

    };
    dispatch_apply(modes, globalQueue, block);

    this->symmetrize();
}

I understand that there might be some scoping issues when I'm accessing array elements like a._c1m[imodes2+jmodes+k], for instance (i.e. I might need to throw some pointers in there or something), but here's the real problem: when I declare NSIntegers like mi or just the looping indices j and k, for instance, the compiler gives me a ton of errors like the following:

'NSInteger op::mi' is not a static member of 'class op'

This has only happened to me for this member function--I implemented almost exactly the same technique on a friend function (with the same NSInteger declarations in the scope of the block) and it worked just fine.

The only fix I've been able to think up is declaring all of my looping variables outside the block as pointers and then dereferencing them in the scope of the block, but this strikes me as kind of a hack. Anybody know what's going on here?

Thanks in advance for your help!

4

1 回答 1

1

这似乎是 Apples gcc 中的编译器错误。您不能在 C++ 成员函数中使用自己的变量创建块文字,即使它们是静态的。因此,现在您必须将此代码移出friend函数并将错误报告给 Apple。

于 2010-09-09T19:14:42.780 回答