views:

91

answers:

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[i*modes2+j*modes+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!

A: 

This seems to be a compiler bug in Apples gcc. You cannot create block literals with their own variables inside C++ member functions, even if they are static. So for now you have to move this code out to a friend function and report the bug to Apple.

Sven