public inbox for gcc-bugs@sourceware.org help / color / mirror / Atom feed
From: "sir_nawaz959 at yahoo dot com" <gcc-bugzilla@gcc.gnu.org> To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/59070] New: Captured object is being moved from the lambda on returning it. Date: Sun, 10 Nov 2013 18:01:00 -0000 [thread overview] Message-ID: <bug-59070-4@http.gcc.gnu.org/bugzilla/> (raw) 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.
next reply other threads:[~2013-11-10 18:01 UTC|newest] Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top 2013-11-10 18:01 sir_nawaz959 at yahoo dot com [this message] 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
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=bug-59070-4@http.gcc.gnu.org/bugzilla/ \ --to=gcc-bugzilla@gcc.gnu.org \ --cc=gcc-bugs@gcc.gnu.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
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).