Movatterモバイル変換


[0]ホーム

URL:



This page is a snapshot from the LWG issues list, see theLibrary Active Issues List for more information and the meaning ofC++20 status.

3077.(push|emplace)_back should invalidate theend iterator

Section: 23.3.13.5[vector.modifiers]Status:C++20Submitter: Casey CarterOpened: 2018-03-10Last modified: 2021-02-25

Priority:3

View all otherissues in [vector.modifiers].

View all issues withC++20 status.

Discussion:

23.3.13.5[vector.modifiers] paragraph 1 specifies thatemplace_backandpush_back do not invalidate iterators before the insertion point whenreallocation is unnecessary:

Remarks: Causes reallocation if the new size is greater than the old capacity.Reallocation invalidates all the references, pointers, and iterators referring tothe elements in the sequence. If no reallocation happens, all the iterators andreferences before the insertion point remain valid. […]
This statement is redundant, given the blanket wording in23.2.2[container.requirements.general] paragraph 12:
Unless otherwise specified (either explicitly or by defining a function in termsof other functions), invoking a container member function or passing a containeras an argument to a library function shall not invalidate iterators to, or changethe values of, objects within that container.
It seems that this second sentence (1) should be a note that reminds us that theblanket wording applies here when no reallocation occurs, and/or (2) actuallyintends to specify that iterators at and after the insertion point are invalidated.

Also, it seems intended that reallocation should invalidate theenditerator as well.

[2018-06-18 after reflector discussion]

Priority set to 3

Previous resolution [SUPERSEDED]:

  1. Edit 23.3.13.5[vector.modifiers] as indicated:

    -1-Remarks:Invalidates the past-the-end iterator. Causesreallocation if the new size is greater than the old capacity. Reallocationinvalidates all the references, pointers, and iterators referring to the elementsin the sequence.[Note: If no reallocation happens, all theiterators and references before the insertion point remainvalid.end note] If an exception is thrown […]

[2018-11-28 Casey provides an updated P/R]

Per discussion in the prioritization thread on the reflector.

[2018-12-01 Status to Tentatively Ready after seven positive votes on the reflector.]

Proposed resolution:

This wording is relative to the post-San Diego working draft.

  1. Change 27.4.3.5[string.capacity] as indicated:

    void shrink_to_fit();

    -11-Effects:shrink_­to_­fit is a non-binding request to reducecapacity() tosize(). [ Note: The request isnon-binding to allow latitude for implementation-specific optimizations.— end note ] It does not increasecapacity(), but mayreducecapacity() by causing reallocation.

    -12-Complexity:If the size is not equal to the old capacity,linear in the size of the sequence; otherwise constant.

    -13-Remarks: Reallocation invalidates all the references, pointers, anditerators referring to the elements in the sequence, as well as thepast-the-end iterator.[Note: If no reallocation happens,they remain valid.end note ]

  2. Change 23.3.5.3[deque.capacity] as indicated:

    void shrink_to_fit();

    -5-Requires:T shall beCpp17MoveInsertableinto*this.

    -6-Effects:shrink_­to_­fit is a non-binding request to reducememory use but does not change the size of the sequence. [ Note: Therequest is non-binding to allow latitude for implementation-specificoptimizations. —end note ] Ifthe size is equal tothe old capacity, or if an exception is thrown other than bythe move constructor of a non-Cpp17CopyInsertableT, then there are no effects.

    -7-Complexity:If the size is not equal to the old capacity,linear in the size of the sequence; otherwise constant.

    -8-Remarks:shrink_to_fitIf the size is notequal to the old capacity, then invalidates all the references, pointers,and iterators referring to the elements in the sequence, as well asthe past-the-end iterator.

  3. Change 23.3.13.3[vector.capacity] as indicated:

    void reserve(size_type n);

    […]

    -7-Remarks: Reallocation invalidates all the references, pointers, anditerators referring to the elements in the sequence, as well as thepast-the-end iterator.[Note: If no reallocation happens,they remain valid. —end note ] No reallocation shall take placeduring insertions that happen after a call toreserve() untilthetime when an insertion would make the size of the vector greater than thevalue ofcapacity().

    void shrink_to_fit();

    […]

    -10-Complexity:If reallocation happens, linear in the sizeof the sequence.

    -11-Remarks: Reallocation invalidates all the references, pointers, anditerators referring to the elements in the sequence, as well as thepast-the-end iterator.[Note: If no reallocation happens,they remain valid.endnote ]

  4. Change 23.3.13.5[vector.modifiers] as indicated:

    -1-Remarks: Causes reallocation if the new size is greater than the oldcapacity. Reallocation invalidates all the references, pointers, and iteratorsreferring to the elements in the sequenceas well as the past-the-enditerator. If no reallocation happens,all the iterators andreferencesthen references, pointers, and iterators before theinsertion point remain validbut those at or after the insertion point,including the past-the-end iterator, are invalidated.If an exception is thrown […]

    -2-Complexity:The complexity isIf reallocationhappens, linear in the number of elements of the resulting vector;otherwise linear in the number of elements inserted plus the distance tothe end of the vector.


[8]ページ先頭

©2009-2025 Movatter.jp