Skip to content
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

Closed
wants to merge 1 commit into from

Conversation

Quuxplusone
Copy link
Contributor

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".

@Quuxplusone Quuxplusone force-pushed the invalidation branch 2 times, most recently from 5abf14f to f1d3704 Compare February 6, 2018 21:52
@jensmaurer
Copy link
Member

jensmaurer commented Feb 6, 2018

I would prefer to unify all "If /reallocation/ occurs, this happens" sentences and put a merged general sentence in the front matter for vector. Then, the individual functions can simply guarantee extra properties for the "if no reallocation occurs..." cases. Maybe something like "Reallocation (xref) may occur; if no reallocation occurs..."

\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.
Copy link
Contributor Author

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.
Copy link
Member

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?

Copy link
Contributor Author

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. :)

@zygoloid zygoloid added the lwg Issue must be reviewed by LWG. label Feb 12, 2018
@zygoloid
Copy link
Member

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.
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
Copy link
Contributor

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.

Copy link
Contributor Author

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?

Copy link
Contributor

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 ...

Copy link
Contributor Author

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.

Copy link
Contributor

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.

@Quuxplusone
Copy link
Contributor Author

Abandoning in favor of the new P/R on LWG 3077.

@Quuxplusone Quuxplusone closed this Dec 1, 2018
@Quuxplusone Quuxplusone deleted the invalidation branch December 1, 2018 02:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lwg Issue must be reviewed by LWG.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants