Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++.moderated > #7334

Re: reusing objects after std::vector.emplace

Message-ID <d6ft6bFc8miU1@mid.individual.net> (permalink)
Newsgroups comp.lang.c++.moderated
From Bo Persson <bop@gmb.dk>
Subject Re: reusing objects after std::vector.emplace
Organization unknown
References <ygawpvib7k1.fsf@panther.akutech-local.de>
Date 2015-09-23 13:34 -0600

Show all headers | View raw


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 <vector>
>      #include <string>
>      #include <sstream>
>
>      void split_string(const std::string & s, std::vector<std::string> &
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! ]

Back to comp.lang.c++.moderated | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

reusing objects after std::vector.emplace Ralf Fassel <ralfixx@gmx.de> - 2015-09-23 07:27 -0600
  Re: reusing objects after std::vector.emplace Bo Persson <bop@gmb.dk> - 2015-09-23 13:34 -0600
  Re: reusing objects after std::vector.emplace Öö Tiib <ootiib@hot.ee> - 2015-09-23 13:34 -0600

csiph-web