Path: csiph.com!xmission!news.glorb.com!peer02.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!Xl.tags.giganews.com!border1.nntp.dca1.giganews.com!nntp.giganews.com!local2.nntp.dca.giganews.com!news.giganews.com.POSTED!not-for-mail NNTP-Posting-Date: Wed, 23 Sep 2015 13:40:01 -0500 Return-Path: Sender: lang-cpp-request@vandevoorde.com Approved: c.l.c.m@bazarov.com Message-ID: Newsgroups: comp.lang.c++.moderated From: Bo Persson Subject: Re: reusing objects after std::vector.emplace Organization: unknown References: Content-Type: text/plain; charset=windows-1252; format=flowed X-Original-Date: Wed, 23 Sep 2015 17:01:30 +0200 X-Submission-Address: lang-cpp-submit@vandevoorde.com Date: Wed, 23 Sep 2015 13:34:25 CST Lines: 46 X-Usenet-Provider: http://www.giganews.com X-Trace: sv3-RcSg+ByPbMruZQb09/5dSqdtXUmPrTHzsqeV6glQtnnE3w/FFb+alULgDFZkdKw+q1+VZGVHHXyyjmw!iN5ht05tK4z9dqU2tEtwjbPjOMuVgSAlLaqNDG+8NFnGnr9MKEobMZZ+rx4= X-Complaints-To: abuse@giganews.com X-DMCA-Notifications: http://www.giganews.com/info/dmca.html X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.40 X-Original-Bytes: 2445 X-Received-Bytes: 2557 X-Received-Body-CRC: 2602360652 Xref: csiph.com comp.lang.c++.moderated:7334 On 2015-09-23 15:27, Ralf Fassel wrote: > > Having not understood completely and absolutely in all depth the concept > of rvalue references, I hope the following is not an FAQ... > > My understanding was that after an move operation on an object, one > should not further deal with it: > > #include > #include > #include > > void split_string(const std::string & s, std::vector & v) { > std::istringstream sstrm(s); > > std::string str; > // here we're reusing str after the emplace_back, is this ok? > while(sstrm >> str) v.emplace_back(str); > } > > Should this loop better be > > while (1) { > std::string str; > if (!(sstrm >> str)) break; > v.emplace_back(str); > } > > Somehow I feel that the >> operator in the first example could do > nothing worse than the DTOR in the second, so both should be ok? > The original code is ok. The moved from string is defined to be in a consistent state and can be assigned a new value. Bo Persson -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]