#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'
Following code doesn't compile in CLang:
with error:
error: reference to local variable 'a' declared in enclosing function 'main'
See example inside GodBolt.
But it compiles in GCC.
Code starts to compile in CLang after 3 types of modifications:
1) Either change
auto F = [&](auto f){
toauto F = [&a](auto f){
, i.e. do explicit capture of&a
.2) Or remove argument of F, i.e. change
auto F = [&](auto f){
toauto F = [&]{
, here(auto f)
removed (also call F just asF();
).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.