[Bug c++/59070] New: Captured object is being moved from the lambda on returning it.
sir_nawaz959 at yahoo dot com
gcc-bugzilla@gcc.gnu.org
Sun Nov 10 18:01:00 GMT 2013
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=59070
Bug ID: 59070
Summary: Captured object is being moved from the lambda on
returning it.
Product: gcc
Version: 4.8.1
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: sir_nawaz959 at yahoo dot com
I'm using GCC 4.8.1
Here is the code which reproduces this bug:
std::vector<std::string> items {"default"};
auto add = [=](std::string item) mutable
{ items.push_back(item); return items; };
std::cout << add("one") << std::endl;
std::cout << add("two") << std::endl;
std::cout << add("three") << std::endl;
Imagine that operator<< is overloaded for std::vector which all items on ONE
line. The expected out is:
default one
default one two
default one two three
But it actually outputs this:
default one
two
three
So it seems on the first return, the captured vector is moved.
However, if I define the lambda as:
auto add = [=](std::string item) mutable
{ items.push_back(item); auto & x = items; return x; } ;
OR as:
auto add = [=](std::string item) mutable
{ auto & x= items; x.push_back(item); return x; } ;
It prints the expected output.
More information about the Gcc-bugs
mailing list