New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Harmonize wording about reallocation invalidating pointers in vector
#1907
Conversation
5abf14f
to
f1d3704
Compare
I would prefer to unify all "If /reallocation/ occurs, this happens" sentences and put a merged general sentence in the front matter for |
source/strings.tex
Outdated
\remarks | ||
Reallocation invalidates all the references, pointers, and iterators | ||
referring to the elements in the sequence, as well as the past-the-end iterator. | ||
If no reallocation takes place, they remain valid. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jensmaurer: Your comment about "front matter for vector
reminded me that shrink_to_fit
is also a member function of basic_string
(as well as deque
, which I already hit, above).
If no reallocation happens, all references, pointers, and iterators | ||
referring to elements before the insertion point remain valid; all other | ||
references, pointers, and iterators referring to elements in the sequence, | ||
as well as the past-the-end iterator, are invalidated. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are the changes here justified by existing normative wording, or merely by this being the obvious (but unstated) intent of the existing rules?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The addition of "as well as the past-the-end iterator" is justified by (1) harmonizing with the existing normative wording for "reallocation" given in shrink_to_fit
, and (2) in practice, vendors do invalidate it.
The addition of "all other foos are invalidated" is justified by (1) assuming that this merely clarifies an implication-by-omission in the old wording, and (2) in practice, vendors do invalidate them.
IMHO this is good enough justification to count as "editorial," but obviously you'd know better than I would. :)
I'm pretty sure this is right, but let's ask LWG to take a quick look just in case. |
`reserve`, `shrink_to_fit`, and `push_back/emplace_back/insert` all use versions of the same wording. This eliminates unintentional differences between their wordings. Prompted by Hubert Tong's mail "[isocpp-lib] Validity of vector iterators after non-reallocating insertion". Open question: `vector::shrink_to_fit` is forbidden to increase `capacity()`. `vector::shrink_to_fit` is permitted to decrease `capacity()` via reallocation. Is `vector::shrink_to_fit` permitted to keep `capacity()` the same, via reallocation? That is, should we add new wording so that `shrink_to_fit` will be forbidden to invalidate any iterators, whenever `capacity()==size()`? Iterators not mentioned here are generally not invalidated, because of http://eel.is/c++draft/container.requirements.general#12 . I'm keeping the explicit mentions in notes, but I kind of expect those notes to disappear "editorially" in the end.
20432e3
to
d9c534d
Compare
referring to the elements in the sequence. | ||
If no reallocation happens, all the iterators and references before the insertion point remain valid. | ||
referring to elements in the sequence, as well as the past-the-end iterator. | ||
If no reallocation happens, all references, pointers, and iterators |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This bit is normative duplication.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, but it's part of a sentence where the second part is not duplication. Can you think of a way to re-word it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Invalidates all references, pointers, and iterators from the insertion point forward, including the
past-the-end iterator. If the new size is greater than the old capacity, causes reallocation which
additionally invalidates all the references, pointers, and iterators referring to elements in the
sequence which precede the insertion point. \begin{note} When no reallocation occurs, references,
pointers, and iterators before the insertion point remain valid. \end{note} If an exception is
thrown ...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess I'm concerned about the potential for misreading "from the insertion point" as exclusive rather than inclusive. (Also, "forward" might better be "onward"? I guess that doesn't matter as it's hard to misread.)
My legalistic eye doesn't like how Casey's wording drops the "referring to elements" part, or uses "including" instead of "as well as"... but nonetheless, I would be fine with Casey's wording making it into the standard.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By all means, suggest alternatives. We do WG21 and ourselves a favor by banging out wording upon which we can agree before asking LWG to rubber stamp it.
e3dbfe2
to
1a21a65
Compare
Abandoning in favor of the new P/R on LWG 3077. |
reserve
,shrink_to_fit
, andpush_back/emplace_back/insert
all use versions of the same wording. This eliminates unintentional
differences between their wordings.
Prompted by Hubert Tong's mail "[isocpp-lib] Validity of vector
iterators after non-reallocating insertion".