Re:Don’t blindly prefer emplace_back to push_back
by SloopJon on Thursday March 04, 2021 @01:45PM
(#26346405)
Actually, he does discuss this in section 4b. He doesn't come out as strongly in favor of it, "The jury is still out on whether to recommend this one wholesale," although he does list what he considers some advantages. He also acknowledges in section 6 that it doesn't work for non-moveable types.
Despite my tendencies towards almost-always auto, I can't say that I consistently use auto x = type{init}, but there is certainly nothing wrong with it.
Re:Don’t blindly prefer emplace_back to push_back
by gpderetta on Thursday March 04, 2021 @02:52PM
(#26347292)
> He also acknowledges in section 6 that it doesn't work for non-moveable types.
Actually these days it does. Copy/Move elision is mandated by the language in this case and no valid copy/move constructor is required. Similarly is no possible to return non-movable non-copyable types from functions which opens the possibility of interesting code patternes.
He gives an example using aggregate initialization, but that's not the same thing:
He makes the case that it's nicer to use auto pretty much wherever we can, to avoid repeating ourselves regarding type, and to avoid unexpected conversions. The auto keyword doesn't apply in our case, there's no way to use auto to simplify a statement like: