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

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).