Skip to content

[Clang] Incorrect error on capture list + lambda. #52720

Closed
@tcanabrava

Description

@tcanabrava
~ ⌚ 11:24:54
$ clang --version
clang version 13.0.0
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin

The following toy code generates the following error.

#include <iostream>

int main() {
    for (auto [test, enum_thing] : std::initializer_list<std::pair<int, int>> { {1,2}}) {
        [test, enum_thing] {
            std::cout << test << " " << enum_thing << std::endl;
        } ();
    }
}
~ ⌚ 11:23:47
$ clang++ -o test test.cpp -std=c++17
test.cpp:5:10: error: 'test' in capture list does not name a variable
        [test, enum_thing] {
         ^
test.cpp:5:16: error: 'enum_thing' in capture list does not name a variable
        [test, enum_thing] {
               ^
test.cpp:6:26: error: reference to local binding 'test' declared in enclosing function 'main'
            std::cout << test << " " << enum_thing << std::endl;
                         ^
test.cpp:4:16: note: 'test' declared here
    for (auto [test, enum_thing] : std::initializer_list<std::pair<int, int>> { {1,2}}) {
               ^
test.cpp:6:41: error: reference to local binding 'enum_thing' declared in enclosing function 'main'
            std::cout << test << " " << enum_thing << std::endl;
                                        ^
test.cpp:4:22: note: 'enum_thing' declared here
    for (auto [test, enum_thing] : std::initializer_list<std::pair<int, int>> { {1,2}}) {
                     ^
4 errors generated.

This is something that I use constantly with Qt based code.
Testing with g++ 11.0 and the code compiles correctly

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:frontendLanguage frontend issues, e.g. anything involving "Sema"

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions