3 ms·
The practical way I look at this is: In the vec.push_back(Widget(a, b, c)) case the Widget is constructed first, then it gets pushed to the container. At this
by daemin 2y ago
The practical way I look at this is:
In the vec.push_back(Widget(a, b, c)) case the Widget is constructed first, then it gets pushed to the container. At this point the container checks if it has enough storage and expands its storage if it needs to. Then the Widget is copied/moved into the containers storage. So the ordering would be: construct, check, resize, move/copy.
While in the vec.emplace_back(a, b, c) case the container can check if it has space first before constructing the Widget directly inside the container. So the ordering would be: check, resize, construct.
So you would need some exceptionally special circumstances for this conversion from the push case to the emplace case to occur.