'When is move assignment called for std::deque emplace()?
As per the description of emplace() method in both std::vector and std::deque -
if the required location has been occupied by an existing element, the inserted element is constructed at another location at first, and then move assigned into the required location.
NOTE: This is true, only when std::vector reallocation doesn't happen on emplace.
When I move construct by emplacing one more element to the vector emplace() example, I see the description of emplace() stands true (when reallocation doesn't happen on emplace). i.e. The last element in the container is move constructed, rest all are move assigned.
NOTE: create a new object A four{"four"}, then move construct by emplace
https://en.cppreference.com/w/cpp/container/vector/emplace
Understandbly the o/p is different for std::deque in this link
https://en.cppreference.com/w/cpp/container/deque/emplace
What conditions would generate the move assignment o/p in std::deque, as it does in std::vector on emplace()?
Solution 1:[1]
To be clear, I don't think that the behaviour here is is guaranteed by the standard. It's just a practical behaviour that arises from the implementation.
You may be able to reproduce the assignment by emplacing to the middle of the deque i.e. somewhere that isn't either of the ends of the deque. In your short example, you are emplacing to the front end, in which case it happens to be possible to construct the element in-place, without needing an extra assignment.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | eerorika |
