I am really confused about std::vector::emplace_back
. I run the following code:
struct Thing {
explicit Thing(std::string name) : name_{std::move(name)} {
std::cout << "Constructed a thing called " << name_ << std::endl;
}
~Thing() {
std::cout << "Deconstructed a thing called " << name_ << std::endl;
};
std::string name_;
};
int main() {
std::vector<Thing> things{Thing("A")};
std::cout << "Size: " << things.size() << std::endl;
things.emplace_back("B");
std::cout << "Size: " << things.size() << std::endl;
}
and get this output:
Constructed a thing called A
Deconstructed a thing called A
Size: 1
Constructed a thing called B
Deconstructed a thing called A
Size: 2
Deconstructed a thing called A
Deconstructed a thing called B
Why on earth does things.emplace_back("B")
invoke the deconstructor of the thing called A?
Aucun commentaire:
Enregistrer un commentaire