←back to thread

178 points todsacerdoti | 1 comments | | HN request time: 0.346s | source
Show context
TonyTrapp ◴[] No.26340399[source]
Don’t blindly prefer emplace_back to push_back*

*when using it incorrectly. The premise of of emplace_back is that you use it for calling the constructor of the object in place. Obviously it won't help you if you call a copy constructor instead. I find this article a bit pointless. Clang's suggestion was spot-on and emplace_back would have (potentially) helped if the suggestion was actually followed correctly.

replies(4): >>26340422 #>>26340494 #>>26340600 #>>26341355 #
1. xKingfisher ◴[] No.26340422[source]
It's not bad advice. I see many code reviews that use emplace_back because it's 'faster'.

Though I think the abseil article does a better job of explaining why: https://abseil.io/tips/112

[Googler; unaffiliated with Abseil(sadly)]