I understand the difference between the two function variants.
My question is: should I normally use good old push_*()
version and only switch to emplace_*()
when my profiler tells me this will benefit performance (that is, do not optimise prematurely)? Or should I switch to using emplace_*()
as the default (perhaps not to pessimise the code unnecessarily - similar to i++
vs ++i
in for
loops)?
Is any of the variants more universal than the other (that is, imposes less constraints on the type being inserted) in realistic non-contrived use cases?
While writing the code I would not worry about performance. Performance is for later when you already have code that you can profile.
I'd rather worry about expressiveness of the code. Roughly speaking,
push_back
is for when you have an element and want to place a copy inside the container.emplace_back
is to construct the element in place.Consider what has the lower "wtf-count":