Skip to content

<deque>: shrink_to_fit() should follow the Standard #4072

@StephanTLavavej

Description

@StephanTLavavej

WG21-N4958 [deque.capacity]/6:

Effects: shrink_to_fit is a non-binding request to reduce memory use but does not change the size of the sequence.
[Note 1: The request is non-binding to allow latitude for implementation-specific optimizations. - end note]
If the size is equal to the old capacity, or if an exception is thrown other than by the move constructor of a non-Cpp17CopyInsertable T, then there are no effects."

We unconditionally move, which is a bug:

deque _Tmp(_STD make_move_iterator(begin()), _STD make_move_iterator(end()));

(#4071 is patching this line in an unrelated way.)

vector provides its strong guarantees correctly, like this (note that it doesn't directly use move_if_noexcept()):

STL/stl/inc/vector

Lines 837 to 842 in 283cf32

if (_Whereptr == _Mylast) { // at back, provide strong guarantee
if constexpr (is_nothrow_move_constructible_v<_Ty> || !is_copy_constructible_v<_Ty>) {
_Uninitialized_move(_Myfirst, _Mylast, _Newvec, _Al);
} else {
_Uninitialized_copy(_Myfirst, _Mylast, _Newvec, _Al);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingfixedSomething works now, yay!

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions