Search code examples
c++parallel-processingppl

Why is class member variable not allowed to be [ &A, &B ] in PPL


Before compile VS says that

Error member "test::A" is not a variable

Error member "test::B" is not a variable

Code:

#include <iostream>
#include <ppl.h>

using namespace concurrency;
using namespace std;

class test
{
        static double A[ 3 ][ 3 ];
        static double B[ 3 ][ 3 ];
public:
        int test_function();
};

double test::A[ 3 ][ 3 ] = { {  0.7,  -0.2,   -1   },
                             { -4,    -2,     -2   },
                             { -0.4,   1.7,   -1.8 } };

double test::B[ 3 ][ 3 ] = { {  0.6,  -1.2,    1.1 },
                             {  2,     3,     -2   },
                             { -1,     0.05,   0.05} };

int test::test_function()
{
    parallel_for ( 0, 100, [ &A, &B ]( int y ) {
        for ( int x = 0; x < 100; x++ ) {

            for ( int i = 0; i < 3; i++ )
                for ( int j = 0; j < 3; j++ )
                     A[ j ][ i ] += A[ j ][ i ] * B[ j ][ i ];

        }
    } );
}

int main()
{
        return 0;
}

Error:

'test::A': a lambda capture variable must be from an enclosing function scope

'test::B': a lambda capture variable must be from an enclosing function scope

what should I do?


Solution

  • Capturing static makes no sense because they're class static. A lambda defined within a function has the same accessibility as the function it is defined within. Thus, variables that are visible within that function (like class privates) are visible within the lambda.

    Class static members will still exist, even if the function is passed elsewhere or outlives the current scope.

    So simply use [] in your lambda instead of [ <stuff> ].