3 ms·
It is better to use push_back over emplace to be explicit about which constructor will be called.
by codesushi42 7y ago
It is better to use push_back over emplace to be explicit about which constructor will be called.
- Koshkin 7y agoBut emplace() is already as explicit about it as it gets.
- codesushi42 7y agoNuh uh. If you're not careful, it will call an implicit constructor.
- wrsh07 7y ago+1, Google suggests doing this as well: https://abseil.io/tips/112 https://abseil.io/tips/112 > So in general, if both push_back() and emplace_back() would work with the same arguments, you should prefer push_back(), and likewise for insert() vs. emplace().
- daemin 7y agoThat's an interesting point the tip makes. Is there guidance on how to use the emplace_back() added to c++17 which returns a reference to the constructed element? The reference returning emplace_back() is used frequently in the code to construct a new element of a struct and then fill in its members, as opposed to creating a new struct then push_back() to copy the memory in.