Bernard Gressing
Bernard Gressing

Reputation: 57

error when taking a reference to a structured binded variable in a lambda

I have the following bit of C++20 code:

#include <map>
#include <string>

void something()
{
    std::map<std::string,int> m;

    int j = 123;

    for (auto& [s,i] : m)
    {

        const auto foo = [&](const auto& j) -> bool
        {
            return j == i;
        };

        if (foo(j))
            break;
    }
}

When I compile it with GCC it works fine, however clang gives the following error:

error: reference to local binding 'i' declared in enclosing function 'something'
            return j == i;

godbolt link: https://godbolt.org/z/nveqscoqn

Which compiler is correct in this situation?

Upvotes: 4

Views: 91

Answers (0)

Related Questions