llvm/llvm-project

Specific structure name can cause shadowing of globally defined lambda, causing "struct::operator()" to be called instead of "lambda::operator()"

Open

#134.049 geöffnet am 2. Apr. 2025

Auf GitHub ansehen
 (24 Kommentare) (0 Reaktionen) (0 zugewiesene Personen)C++ (10.782 Forks)batch import
c++clang:frontendgood first issuelambda

Repository-Metriken

Stars
 (26.378 Stars)
PR-Merge-Metriken
 (Durchschn. Merge 1T 2h) (1.000 gemergte PRs in 30 T)

Beschreibung

Defining structure with naming convention used by the compiler for lambda expressions ("$_<num>") causes compiler to use structure's "operator()" instead of the one defined for a global lambda(s).

In the following code:

struct $_0
{
    int c;
    void operator()() const {std::cout << __func__ << " " << c <<" from struct\n";}
};

auto lambda = [](){std::cout << __func__ << " from lambda\n";};

int main()
{
    lambda();
}

Call to "lambda()" invokes "$_0::operator()" instead of the expected lambda: Image

If the "$_0::operator()" makes use of any memory, then calls to lambdas result in memory access operations. "-fsanitize=address" allows for detection of them: godbolt example

Contributor Guide