1

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?

Community
  • 1
  • 1
user2055437
  • 67
  • 1
  • 13

1 Answers1

2

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

BЈовић
  • 62,405
  • 41
  • 173
  • 273
Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
  • Except they're private globals. Do they still have access? – Pubby Feb 13 '13 at 08:21
  • @Pubby: Sorry; missed that. But the logic remains the same. And yes, they have access; a lambda from a class member is considered a class member. – Nicol Bolas Feb 13 '13 at 08:22
  • how can protect A, is it global and shared item? – user2055437 Feb 13 '13 at 08:25
  • 1
    @user2055437: What do you need to protect it from? Private variables are accessible from within members (and friends). Your lambda is effectively a member by proxy, since it can access private variables. Not to mention, the lambda is defined in the same location as the member function, so anyone who could sneak some code into the lambda could sneak some code into the member function. There's nothing to protect `A` from. – Nicol Bolas Feb 13 '13 at 08:27
  • i asked wrongly, not protect let's say locked them because in process A array's result different – user2055437 Feb 13 '13 at 08:41
  • @user2055437: If you're talking about mutexes and race conditions, then that's a completely different question. You should ask that with the "Ask Question" button; this current question is about lambdas. Also, it's not clear what this `parallel_for` loop is supposed to do, since each iteration will be computing the exact same thing. – Nicol Bolas Feb 13 '13 at 19:27