[Bug c++/93842] New: generic lambda accesses a variable with with automatic storage duration that wasn't captured by the lambda expression

kuzniar95 at o2 dot pl gcc-bugzilla@gcc.gnu.org
Thu Feb 20 01:00:00 GMT 2020


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

            Bug ID: 93842
           Summary: generic lambda accesses a variable with with automatic
                    storage duration that wasn't captured by the lambda
                    expression
           Product: gcc
           Version: 10.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: kuzniar95 at o2 dot pl
  Target Milestone: ---

The following code:
--------------
int main() {
        constexpr char ch = '=';

        [](auto) { return ch; }; // NOT OK
}
--------------
g++ -Wunused -std=c++14 lambda.cpp

produces:
--------------
lambda.cpp: In function ‘int main()’:
lambda.cpp:2:17: warning: variable ‘ch’ set but not used
[-Wunused-but-set-variable]
  constexpr char ch = '=';
--------------

I believe GCC is doubly wrong here, as this warning isn't right, because 'ch'
is actually used, but primarily it shouldn't compile the code as:
> If a lambda expression (or an instantiation of a generic lambda's function
> call operator) ODR-uses this or any variable with automatic storage duration,
> it must be captured by the lambda expression.


More information about the Gcc-bugs mailing list