Bug 56901 - [4.9 regression] lambda with implicit capture by reference
Summary: [4.9 regression] lambda with implicit capture by reference
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.9.0
: P3 normal
Target Milestone: 4.9.0
Assignee: Jason Merrill
URL:
Keywords: c++-lambda
: 56857 (view as bug list)
Depends on:
Blocks: lambdas
  Show dependency treegraph
 
Reported: 2013-04-10 08:29 UTC by Nathan Ridge
Modified: 2022-03-11 00:32 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2013-04-10 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Nathan Ridge 2013-04-10 08:29:40 UTC
The following code compiles with gcc 4.8 and clang, but not with gcc 4.9:


template <typename>
void foo_impl()
{
    int data;
    auto L = [&](){ return data; };
    [&](){ L(); }();
}

void foo()
{
    foo_impl<int>();
}


The error is:


test.cpp: In instantiation of 'foo_impl() [with <template-parameter-1-1> = int]::__lambda1':
test.cpp:6:12:   required from 'struct foo_impl() [with <template-parameter-1-1> = int]::__lambda1'
test.cpp:6:19:   required from 'void foo_impl() [with <template-parameter-1-1> = int]'
test.cpp:11:19:   required from here
test.cpp:5:14: error: uninitialized const member 'foo_impl() [with <template-parameter-1-1> = int]::__lambda0::__data'
     auto L = [&](){ return data; };
              ^


The error goes away if:
  - foo_impl is made a nontemplate
  - the call to the second lambda is inlined
  - either lambda is made to capture by value instead of reference
Comment 1 Nathan Ridge 2013-04-10 08:30:23 UTC
Tested with r197663.
Comment 2 Jason Merrill 2013-04-11 16:51:12 UTC
Fixed.
Comment 3 Jason Merrill 2013-04-16 21:43:38 UTC
*** Bug 56857 has been marked as a duplicate of this bug. ***