Paragraph C.2.12 of the Annex C (Compatibility) to the C++11 Standard specifies:
Change: Signature changes: resize
Rationale: Performance, compatibility with move semantics.
Effect on original feature: For vector
, deque
, and list
the fill value passed to resize is now passed by
reference instead of by value, and an additional overload of resize has been added. Valid C++ 2003 code
that uses this function may fail to compile with this International Standard.
The old resize()
function was copy-constructing new elements from value
. This makes it impossible to use resize()
when the elements of the vector are default-constructible but non-copyable (you may want to move-assign them later on). This explains the "Compatibility with move semantics" rationale.
Moreover, it may be slow if you do not want any copy to occur, just new elements to be default-constructed. Also, the value
parameter is passed by value in the C++03 version, which incurs in the overhead of an unnecessary copy (as mentioned by TemplateRex in his answer). This explains the "Performance" rationale.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…