<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/58421>58421</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            CLang OpenMP: error capturing by reference variable of double nested lambdas
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          polkovnikov
      </td>
    </tr>
</table>

<pre>
    Following code doesn't compile in CLang:

```cpp
#include <vector>

int main() {
    std::vector<int> a(10);
    auto F = [&](auto f){
        #pragma omp parallel for
        for (int i = 0; i < 10; ++i)
            [&, i]{
                auto x = a[0];
            }();
    };
    F([&](auto i){ return i + 1; });
}
```

with error: `error: reference to local variable 'a' declared in enclosing function 'main'`

See example [inside GodBolt](https://godbolt.org/z/4xa6sT54K).

But it compiles in GCC.

Code starts to compile in CLang after 3 types of modifications:

1) Either change `auto F = [&](auto f){` to `auto F = [&a](auto f){`, i.e. do explicit capture of `&a`.

2) Or remove argument of F, i.e. change `auto F = [&](auto f){` to `auto F = [&]{`, here `(auto f)` removed (also call F just as `F();`).

3) Or remove `#pragma omp parallel for`, i.e. remove OpenMP completely.

All 1)-3) types of modifications lead to compilable code. Of course only 1) is acceptable solution.

</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJytVNtu2zAM_Rr7RWjgS2wnD35okmYP29AB2w_IEp2oUyxDkrNmXz9STlo76IA9zMhFl0PykDx0Y-Sl3hutzS_VHZgwEpg04LooqzxuT73SwFTHtl94d4jyxyjZRcntt0zGj-j760mWq07oAb1E-fYMwhsb5U9TI9V5duIKA6yibM2iajOeM3yclxQif7xZbhGN9owjOk0QH-UTOB-8YXuMtGNRsYmyMip2CAzHLYGnvulBer3lhxNnmBjrueVag2Ythprh8ACxK6KqgvsE44bllqVhHWUYb6MoyMwyRBm5ZFumiNA9idsTaL4G9xxNEsLmH2CjajfW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