jeudi 24 août 2017

Clang compilation error: "variable 'array' with variably modified type cannot be captured in lambda expression

In the code listed below, "LambdaTest" fails with the following error on Clang only:

shared/LambdaTest.cpp:8:31: error: variable 'array' with variably 
modified type cannot be captured in a lambda expression
    auto myLambdaFunction = [&array]()
                          ^
shared/LambdaTest.cpp:7:9: note: 'array' declared here
    int array[length];

The function "LambdaTest2" which passes the array as a parameter instead of capturing compiles fine on G++/Clang.

// Compiles with G++ but fails in Clang
void LambdaTest(int length)
{
    int array[length];
    auto myLambdaFunction = [&array]()
    {
        array[0] = 2;
    };
    myLambdaFunction();
}

// Compiles OK with G++ and Clang
void LambdaTest2(int length)
{
    int array[length];
    auto myLambdaFunction = [](int* myarray)
    {
        myarray[0] = 2;
    };
    myLambdaFunction(array);
}

Two questions:

  1. What does the compiler error message "variable 'array' with variably modified type cannot be captured in a lambda expression" mean?

  2. Why does LambdaTest fail to compile on Clang and not G++?

Thanks in advance.

COMPILER VERSIONS:
*G++ version 4.6.3
*clang version 3.5.0.210790

Aucun commentaire:

Enregistrer un commentaire