[Bug c++/106552] New: GCC accepts invalid redefinition of captured variable inside lambda

jlame646 at gmail dot com gcc-bugzilla@gcc.gnu.org
Mon Aug 8 07:59:30 GMT 2022


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106552

            Bug ID: 106552
           Summary: GCC accepts invalid redefinition of captured variable
                    inside lambda
           Product: gcc
           Version: 11.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: jlame646 at gmail dot com
  Target Milestone: ---

The following invalid(afaik) program is accepted by gcc:
https://godbolt.org/z/sPozd36oY 

int main() {
    int x = 100;
    auto lamb_var = [y = x](){
                    int y = 10; //this is accepted by gcc even though this is a
redefinition of y
                    return y + 1;
    }; 

    assert (lamb_var() == 11);

    return 0; 
}



--------------

In the above program, since we've used `y = x`, this means the non-static data
member will be named `y` and is considered to be in the declarative region of
the lambda expression's compound statement which in turn means that the
statement int y = 10; should produced redefinition error because we're defining
a variable named `y` for the second time in the same region.


More information about the Gcc-bugs mailing list