In Item 41 from Effective Modern C++ Scott Meyers mentions this difference and its impact on the efficiency of emplacement with respect to insertion. I have some doubts about that, but before asking questions about that I need to understand what the difference between these two ways of adding an element is.
Given the code sample from the book
std::vector<std::string> vs;
// add elements to vs
vs.emplace(v.begin(), "xyzzy");
(and leaving aside the fact that all preexisting elements in the container have to be move assigned/move constructed/copy assigned/copy constructed for the all combinations of noexcept-ness of move operations and v.size() == v.capacity()-ness of the container) what are the two scenarios?
- If
emplaceadds the element via move assignment, it means it doesv[0] = std::move(std::string{strarg}); // strarg == "xyzzy", right? - But what is the other case of contructing the element occupied by
v[0]? Is it a placementnew? And how would it look like? I guess it should be similar to the chunk of code at the section Placement new here, but I'm not sure how it would look like in this case.
Aucun commentaire:
Enregistrer un commentaire