f



reusing objects after std::vector.emplace

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?

TNX
R'


-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Ralf
9/23/2015 7:27:48 AM
comp.lang.c++.moderated 10738 articles. 1 followers. allnor (8509) is leader. Post Follow

2 Replies
419 Views

Similar Articles

[PageSpeed] 19

On Wednesday, 23 September 2015 15:30:19 UTC+3, 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:

No. All objects of classes in standard library must remain in valid state
after move. For most classes that state is unspecified but no limits to 
further usage. For some classes however it is specified (like 'unique_ptr'
must be empty after move). 

> 
>     #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);
>     }

Here are no move done at all. It is perfect forwarding so a
copy constructor is called by 'std::allocator_traits::construct'
to emplace 'str' to 'v'. Should be something like:

    while(sstrm >> str) v.emplace_back(std::move(str));

It must be ok, since 'operator>>' there is documented to
overwrite the unspecified state that 'str' has after move.


-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
ISO
9/23/2015 1:34:17 PM
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! ]

0
Bo
9/23/2015 1:34:25 PM
Reply:

Similar Artilces:

Link C++ object with C object
Hi All, I've compiled a cpp file with g++ without linking it and got an object file. Then I've compiled a C file with gcc and got an object file too. Now if I try to link it to an executable (the main method is in the C object) the linker complains about not finding the symbol for a function declared in the C++ object. Somebody told me that the two compilers use different symbol names for the same function names. He said also it would exist something like a "export this function as C style" keyword but I didn't find anything. Could somebody help me with this? bye! Domin...

[Objective-C++] Objective-C wrappers around C++ classes and KVC
Suppose I have a C++ class named Bar, and an Objective-C wrapper for it called BarWrapper that I use so that BarWrapper has a few KVC-compliant methods (essentially, Bar is the model of something I need to link to an NSArrayController through BarWrapper). Now inside Bar I have a std::vector<Bar*> object, and of course I need to make indexed accessors of the BarWrapper* type. Now, I have two issues, both with the NSArrayController that the BarWrappers are connected to: - the first (and the easier one) is deleting BarWrappers: If I remove a BarWrapper from the master list, I ...

Embedding an Objective-C object in a C structure
Yup, i have a quite perver problem : i would like to embed an Objective-C object within a C structure. The reason is the following: i have an old code base that use a kind of 'object oriented' style in C (stuff quite commons in the 80s and 90s), where an 'object', a kind of plugin for the application, is defined in C by extending a struct, like in: struct { struct basic_object obj; ... new fields .. } I would like to migrate the code base toward Objective-C, but keeping compatibility with the existing stuff. I can of course install a second plugin defin...

How to Call Objective C Object from C++ code
Hi, guys: i prepare to build UI of program by cocoa. but in order to make a portable program, some low level code sould write in C++. then i must find some way to call Apple Service or Objective C from C++. Could anyone tell me a way to do that. Best Regard. In article <1143784817.800849.152730@t31g2000cwb.googlegroups.com>, "Mopelee" <kingdry_cn@sina.com.cn> wrote: > Hi, guys: > > i prepare to build UI of program by cocoa. > but in order to make a portable program, some low level code sould > write in C++. then i must find some way to call Apple Ser...

Sending message to Objective-C object from C file
Hi everyone, I am an experienced programmer but quite new in Mac OS programming. I am developing a Cocoa application. I have an old C library files which I want to reuse in this application. Part of that C library file plays an audio file using another Audio library file. I want to replace functionality of that Audio library file with QuickTime. So I have created a Controller Objective-C object that includes QTMovieViewer and can use that to play an audio file. Now my question is how to send message to that Objective-C object from a C file in my old library. Thanks in adva...

C++ objects that act like Java/C# objects.
I've had an idea kicking around in my head regarding how to create a library of classes (templates?) that provide the same kind of functionality as do Java classes which all derive from the UBC Object. There is no UBC in C++, nor will there ever be one (well, there actually /is/ a UBC in C++, but it is a pure abstraction). One feature of user defined Java classes is that they all have a member derived from the java.lang.Class object. The Class member object is part of what provides introspection. I haven't looked at this in a couple years, so the details are a bit hazy for me. Ano...

Objective-C and C++
I'm trying to write the, "Great American MMO" as a way to teach myself Objective-C. I was going to use an open source library to help with the 3D rendering and animation. However, most of them are written in C++, not Objective-C. So I have a couple of questions. 1) Is it more trouble than it is worth to try to get a C++ library working with an Objective-C program? 2) What is a good reference for C++ on the Mac? (I am using the latest version of Xcode and Tiger, if it makes a difference.) Oh, if anyone has a recommendation for an open-source 3D library, I...

Objectives on C and C++
hello, Can ne one tell me the sites where i can solve/download objective questions on C and C++? I want them for preparing for interviews. rudranee@gmail.com wrote: > hello, > Can ne one tell me the sites where i can solve/download objective > questions on C and C++? > I want them for preparing for interviews. If you are looking for a serious job, concentrate on proper writing. 'i' should be capitalized, and silly abbreviations like 'ne' make you look less than professional. ----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==---- http:/...

Deleteing Objects from std::vector
I have a class Scene that has several subclasses: World, Vault, etc. I fill a vector with these classes and then cannot go through and delete them. What's the trick to deleting pointers from a vector? Code: std::vector<Scene*> scenes; void initializeGame() { robot = new Robot("COM2"); player = new Player(); //Add Scenes scenes.push_back(new World(robot, player)); scenes.push_back(new SecurityWires(robot)); scenes.push_back(new Keypad(robot)); scenes.push_back(new Maze(robot, player)); scenes.push_back(new Explosives(robot, player)); scenes.push_back(new Vaul...

[Objective-C++] @defs, objective-c classes, and pod types
Can we assume that objective-C classes are C++ pod types when we do something like the following? @interface Foo { //... } //... - (bool) isEqualToFoo:(Foo*) foo; @end class FooWrapper { @defs(Foo) public: // no constructors as we must preserve podness operator Foo*() { return this; } bool operator==(FooWrapper& rhs) { return [this isEqualToFoo:rhs]; } }; -- I am only a mirage. kelvSYC <kelvSYC@no.email.shaw.ca> wrote: > Can we assume that objective-C classes are C++ pod types when we do > something like the followin...

std::vector and object copying
Hi, .... std::vector<MyClass> v1; v1.push_back(MyClass("abc")); .... The above results in a single call to the 'copy constructor' of MyClass when I use gcc. However, the same code results in 2 calls to the 'copy constructor' when I use Visual C++ (VStudio 2005). VStudio 2005 is the latest from Microsoft and it can't be that bad!. Am I missing some compile time optimization option? Apologies, if you think it is a wrong group to bother with. Thanks. bb wrote: > Hi, > > ... > std::vector<MyClass> v1; > v1.push_back(MyClass("abc&q...

subtracting std::vector objects
Hi, I have two vector<int> objects with the values 1,2,3,4,5,6 and 1,2,4,6 respectfully. Is there a function available within std which will give me a vector<int> object with 3, 5 (subtraction). Thanks, Barry > I have two vector<int> objects with the values 1,2,3,4,5,6 and 1,2,4,6 > respectfully. Is there a function available within std which will give > me a vector<int> object with 3, 5 (subtraction). If you use std::set instead of vector, then you've got std::set_difference > Hi, > > I have two vector<int> objects with the values 1,2,...

C++ objects containing vectors
Hi, I was trying to send c++ objects which contain vector. But it is giving memory fault. Can anyone help? My code is as follows. Thanks in advance. regards, Uzzal. class CVerIns { public: CVerIns(): m_lLocalID(-1), m_lProcessorID(-1){ } virtual ~CVerIns(){ } public: long m_lLocalID; long m_lProcessorID; }; typedef std::vector <CVerIns> VerInsVector; ////////////////////////////////////////////////////////////////////////// class CRSVertex { public: CRSVertex(){ } virtual ~CRSVertex(){ reset();} void reset(); public: long m_lLabel; bool m_isOrg; CVerIns m_insMatPartO...

Is C# really "better" than C++ or C++0x? How about objective-C?
Microsoft thinks C++ is obsolete and C# is "the future". Apple thinks the same for Objective-C. IMO this is all nonsense. What do you think about this? How "better" really they are? And what about C++0x? On Sun, 17 Apr 2011 16:58:37 +0200, "A" <a@a.a> wrote: >Microsoft thinks C++ is obsolete and C# is "the future". Apple thinks the >same for Objective-C. > >IMO this is all nonsense. I agree. > >What do you think about this? How "better" really they are? And what about >C++0x? > Companies like Microsoft ...

Web resources about - reusing objects after std::vector.emplace - comp.lang.c++.moderated

Resources last updated: 1/25/2016 5:12:51 AM