public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/59070] New: Captured object is being moved from the lambda on returning it.
@ 2013-11-10 18:01 sir_nawaz959 at yahoo dot com
2013-11-10 18:27 ` [Bug c++/59070] " redi at gcc dot gnu.org
` (4 more replies)
0 siblings, 5 replies; 6+ messages in thread
From: sir_nawaz959 at yahoo dot com @ 2013-11-10 18:01 UTC (permalink / raw)
To: gcc-bugs
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.
^ permalink raw reply [flat|nested] 6+ messages in thread
* [Bug c++/59070] Captured object is being moved from the lambda on returning it.
2013-11-10 18:01 [Bug c++/59070] New: Captured object is being moved from the lambda on returning it sir_nawaz959 at yahoo dot com
@ 2013-11-10 18:27 ` redi at gcc dot gnu.org
2013-11-10 18:31 ` redi at gcc dot gnu.org
` (3 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: redi at gcc dot gnu.org @ 2013-11-10 18:27 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=59070
Jonathan Wakely <redi at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Status|UNCONFIRMED |WAITING
Last reconfirmed| |2013-11-10
Ever confirmed|0 |1
--- Comment #1 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to Sarfaraz Nawaz from comment #0)
> Here is the code which reproduces this bug:
That code doesn't reproduce anything because it doesn't compile, please provide
*complete* testcases instead of expecting other people to fill in the gaps.
^ permalink raw reply [flat|nested] 6+ messages in thread
* [Bug c++/59070] Captured object is being moved from the lambda on returning it.
2013-11-10 18:01 [Bug c++/59070] New: Captured object is being moved from the lambda on returning it sir_nawaz959 at yahoo dot com
2013-11-10 18:27 ` [Bug c++/59070] " redi at gcc dot gnu.org
@ 2013-11-10 18:31 ` redi at gcc dot gnu.org
2013-11-10 18:32 ` redi at gcc dot gnu.org
` (2 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: redi at gcc dot gnu.org @ 2013-11-10 18:31 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=59070
Jonathan Wakely <redi at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Status|WAITING |RESOLVED
Resolution|--- |WORKSFORME
--- Comment #2 from Jonathan Wakely <redi at gcc dot gnu.org> ---
This is a valid testcase:
#include <iostream>
#include <vector>
#include <string>
struct VecPrinter {
const std::vector<std::string>& v;
};
std::ostream& operator<<(std::ostream& out, VecPrinter vp)
{
for (auto& i : vp.v)
out << i << ' ';
return out;
}
int main()
{
std::vector<std::string> items {"default"};
auto add = [=](std::string item) mutable
{ items.push_back(item); return items; };
std::cout << VecPrinter{ add("one") } << std::endl;
std::cout << VecPrinter{ add("two") } << std::endl;
std::cout << VecPrinter{ add("three") } << std::endl;
}
And it works perfectly with
gcc version 4.8.3 20131029 (prerelease) (GCC)
gcc version 4.9.0 20131002 (experimental) (GCC)
^ permalink raw reply [flat|nested] 6+ messages in thread
* [Bug c++/59070] Captured object is being moved from the lambda on returning it.
2013-11-10 18:01 [Bug c++/59070] New: Captured object is being moved from the lambda on returning it sir_nawaz959 at yahoo dot com
2013-11-10 18:27 ` [Bug c++/59070] " redi at gcc dot gnu.org
2013-11-10 18:31 ` redi at gcc dot gnu.org
@ 2013-11-10 18:32 ` redi at gcc dot gnu.org
2013-11-10 18:54 ` sir_nawaz959 at yahoo dot com
2013-11-10 19:27 ` redi at gcc dot gnu.org
4 siblings, 0 replies; 6+ messages in thread
From: redi at gcc dot gnu.org @ 2013-11-10 18:32 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=59070
--- Comment #3 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to Jonathan Wakely from comment #2)
> And it works perfectly with
> gcc version 4.8.3 20131029 (prerelease) (GCC)
> gcc version 4.9.0 20131002 (experimental) (GCC)
and
gcc version 4.7.4 20131030 (prerelease) (GCC)
^ permalink raw reply [flat|nested] 6+ messages in thread
* [Bug c++/59070] Captured object is being moved from the lambda on returning it.
2013-11-10 18:01 [Bug c++/59070] New: Captured object is being moved from the lambda on returning it sir_nawaz959 at yahoo dot com
` (2 preceding siblings ...)
2013-11-10 18:32 ` redi at gcc dot gnu.org
@ 2013-11-10 18:54 ` sir_nawaz959 at yahoo dot com
2013-11-10 19:27 ` redi at gcc dot gnu.org
4 siblings, 0 replies; 6+ messages in thread
From: sir_nawaz959 at yahoo dot com @ 2013-11-10 18:54 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=59070
Sarfaraz Nawaz <sir_nawaz959 at yahoo dot com> changed:
What |Removed |Added
----------------------------------------------------------------------------
Status|RESOLVED |NEW
Resolution|WORKSFORME |---
--- Comment #4 from Sarfaraz Nawaz <sir_nawaz959 at yahoo dot com> ---
Here is the complete testcase which compiles fine but gives incorrect output:
#include <iostream>
#include <vector>
#include <string>
std::ostream & operator<<(std::ostream & out, std::vector<std::string> const &
items)
{
for(auto const & item : items )
out << item << " ";
return out;
}
int main()
{
std::cout << "\nGCC " << __VERSION__ << std::endl;
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;
}
$ g++-4.8 -std=c++11 -O2 -Wall -pedantic-errors main.cpp && ./a.out
GCC 4.8.1
default one
two
three
which is incorrect output
^ permalink raw reply [flat|nested] 6+ messages in thread
* [Bug c++/59070] Captured object is being moved from the lambda on returning it.
2013-11-10 18:01 [Bug c++/59070] New: Captured object is being moved from the lambda on returning it sir_nawaz959 at yahoo dot com
` (3 preceding siblings ...)
2013-11-10 18:54 ` sir_nawaz959 at yahoo dot com
@ 2013-11-10 19:27 ` redi at gcc dot gnu.org
4 siblings, 0 replies; 6+ messages in thread
From: redi at gcc dot gnu.org @ 2013-11-10 19:27 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=59070
Jonathan Wakely <redi at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Status|NEW |RESOLVED
Known to work| |4.7.4, 4.8.3, 4.9.0
Resolution|--- |FIXED
--- Comment #5 from Jonathan Wakely <redi at gcc dot gnu.org> ---
But I already said it works with current releases, and obviously we don't have
a time machine to fix 4.8.1 now, so there's no point re-opening the bug.
GCC 4.8.2 20131017 (Red Hat 4.8.2-1)
default one
default one two
default one two three
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2013-11-10 19:27 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-11-10 18:01 [Bug c++/59070] New: Captured object is being moved from the lambda on returning it sir_nawaz959 at yahoo dot com
2013-11-10 18:27 ` [Bug c++/59070] " redi at gcc dot gnu.org
2013-11-10 18:31 ` redi at gcc dot gnu.org
2013-11-10 18:32 ` redi at gcc dot gnu.org
2013-11-10 18:54 ` sir_nawaz959 at yahoo dot com
2013-11-10 19:27 ` redi at gcc dot gnu.org
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).