Since C++14, from new#Allocation:
New-expressions are allowed to elide or combine allocations made through replaceable allocation functions. In case of elision, the storage may be provided by the compiler without making the call to an allocation function (this also permits optimizing out unused new-expression). [..]
Note that this optimization is only permitted when new-expressions are used, not any other methods to call a replaceable allocation function: delete[] new int[10];
can be optimized out, but operator delete(operator new(10));
cannot.
And the default allocator used by std::vector
uses the latter, so your suggested optimization is forbidden (since the as-if rule might still apply, but those operators might have been replaced, so it'll be harder to prove that there are no side effects).
If you provide a custom allocator, you might have the expected optimization: Demo.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…