Re:Don't blindly prefer `emplace_back` to `push_back`
by layer8 on Sunday July 28, 2024 @07:10PM
(#41096710)
Maybe construct_back would have been a better name.
Re:Don't blindly prefer `emplace_back` to `push_back`
by jeffreygoesto on Monday July 29, 2024 @02:11AM
(#41098273)
In our pre-C++11 library we had containers with a append_uninitialized_element() and called placement new on that with an explicit ctor. While syntactically phony it had an unambigous semantic. Code review had to check (remember, pre clang-tidy times) that the method way never used alone.
Mentioning `Widget` is definitely more explicit, so I agree with the author that `push_back(Constructor(...))` is usually better. Semantically, it's also usually simpler to think of "push a newly-constructed Widget" vs "construct a Widget in the next element of the vector and lengthen the vector to include it".