c++ - Does moving an element from an STL container remove it from that container? -
c++ - Does moving an element from an STL container remove it from that container? -
i have foobar
class sayhello()
method outputs "well hello there!". if write next code
vector<unique_ptr<foobar>> foolist; foolist.emplace_back(new foobar()); unique_ptr<foobar> myfoo = move(foolist[0]); unique_ptr<foobar> myfoo2 = move(foolist[0]); myfoo->sayhello(); myfoo2->sayhello(); cout << "vector size: " << foolist.size() << endl;
the output is:
well hello there! hello there! vector size: 1
i'm confused why works. shouldn't foolist[0]
become null when first move? why myfoo2
work?
here's foobar
looks like:
class foobar { public: foobar(void) {}; virtual ~foobar(void) {}; void sayhello() const { cout << "well hello there!" << endl; }; };
shouldn't foolist[0] become null when first move?
yes.
why myfoo2 work?
it doesn't; causes undefined behaviour. compiler happens produce code doesn't crash if utilize null pointer phone call non-virtual function doesn't dereference this
.
if alter function follows, clearer what's happening:
void sayhello() const { cout << "well hello there! address " << << endl; } hello there! address 0x1790010 hello there! address 0 vector size: 1
c++ visual-studio-2010 stl move-semantics
Comments
Post a Comment