Title
Severe restriction on basic_string reference counting
Status
cd1
Section
[basic.string]
Submitter
Kevlin Henney

Created on 2000-09-04.00:00:00 last changed 172 months ago

Messages

Date: 2010-10-21.18:28:33

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

Date: 2000-09-04.00:00:00

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.

History
Date User Action Args
2010-10-21 18:28:33adminsetmessages: + msg2043
2000-09-04 00:00:00admincreate