4 ms·
The article makes it more complex than it needs to be. push_back and emplace_back have different goals. The former works with existing objects, the latter gets
by exitb 2y ago
The article makes it more complex than it needs to be. push_back and emplace_back have different goals. The former works with existing objects, the latter gets used when you intend to construct a new one, right in the collection.
It happens to be that you can use emplace_back in place of push_back because copies and moves are just constructor overloads in C++. You shouldn't really use that, as it signals one intent, but does something else.
- binary132 2y agoYes, it’s confusing because of the semantic overlap between push accepting an object reference and _the vector element type’s constructor_ accepting an object reference.