basic_string
reference countingSection: 27.4.3 [basic.string] Status: CD1 Submitter: Kevlin Henney Opened: 2000-09-04 Last modified: 2016-01-28
Priority: Not Prioritized
View other active issues in [basic.string].
View all other issues in [basic.string].
View all issues with CD1 status.
Discussion:
The note in paragraph 6 suggests that the invalidation rules for references, pointers, and iterators in paragraph 5 permit a reference- counted implementation (actually, according to paragraph 6, they permit a "reference counted implementation", but this is a minor editorial fix).
However, the last sub-bullet is so worded as to make a reference-counted implementation unviable. In the following example none of the conditions for iterator invalidation are satisfied:
// first example: "*******************" should be printed twice string original = "some arbitrary text", copy = original; const string & alias = original; string::const_iterator i = alias.begin(), e = alias.end(); for(string::iterator j = original.begin(); j != original.end(); ++j) *j = '*'; while(i != e) cout << *i++; cout << endl; cout << original << endl;
Similarly, in the following example:
// second example: "some arbitrary text" should be printed out string original = "some arbitrary text", copy = original; const string & alias = original; string::const_iterator i = alias.begin(); original.begin(); while(i != alias.end()) cout << *i++;
I have tested this on three string implementations, two of which were reference counted. The reference-counted implementations gave "surprising behavior" because they invalidated iterators on the first call to non-const begin since construction. The current wording does not permit such invalidation because it does not take into account the first call since construction, only the first call since various member and non-member function calls.
Proposed resolution:
Change the following sentence in 21.3 paragraph 5 from
Subsequent to any of the above uses except the forms of insert() and erase() which return iterators, the first call to non-const member functions operator[](), at(), begin(), rbegin(), end(), or rend().
to
Following construction or any of the above uses, except the forms of insert() and erase() that return iterators, the first call to non- const member functions operator[](), at(), begin(), rbegin(), end(), or rend().