views:

267

answers:

1

For a personal project I have been implementing my own libstdc++. Bit by bit, I've been making some nice progress. Usually, I will use examples from http://www.cplusplus.com/reference/ for some basic test cases to make sure that I have the obvious functionality working as expected.

Today I ran into an issue with std::basic_string::replace, specifically with the iterator based versions using the example copied verbatim from the site (http://www.cplusplus.com/reference/string/string/replace/) (I've added a comment to point out the lines in question):

// replacing in a string
#include <iostream>
#include <string>
using namespace std;

int main ()
{
  string base="this is a test string.";
  string str2="n example";
  string str3="sample phrase";
  string str4="useful.";

  // function versions used in the same order as described above:

  // Using positions:                 0123456789*123456789*12345
  string str=base;                // "this is a test string."
  str.replace(9,5,str2);          // "this is an example string."
  str.replace(19,6,str3,7,6);     // "this is an example phrase."
  str.replace(8,10,"just all",6); // "this is just a phrase."
  str.replace(8,6,"a short");     // "this is a short phrase."
  str.replace(22,1,3,'!');        // "this is a short phrase!!!"

  // Using iterators:                      0123456789*123456789*
  string::iterator it = str.begin();   //  ^
  str.replace(it,str.end()-3,str3);    // "sample phrase!!!"

  // *** this next line and most that follow are illegal right? ***

  str.replace(it,it+6,"replace it",7); // "replace phrase!!!"
  it+=8;                               //          ^
  str.replace(it,it+6,"is cool");      // "replace is cool!!!"
  str.replace(it+4,str.end()-4,4,'o'); // "replace is cooool!!!"
  it+=3;                               //             ^
  str.replace(it,str.end(),str4.begin(),str4.end());
                                       // "replace is useful."
  cout << str << endl;
  return 0;
}

In my version of replace is implemented in terms of a temporary string which I create then swap with *this. This clearly invalidates any iterators. So am I correct that the example is invalid? because it stores iterators, does a replace and then uses the iterators again?

My copy of the standard (ISO 14882:2003 - 21.3p5) says:

References, pointers, and iterators referring to the elements of a basic_string sequence may be invalidated by the following uses of that basic_string object:

- As an argument to non-member functions swap() (21.3.7.8), 
  operator>>() (21.3.7.9), and getline() (21.3.7.9).
- As an argument to basic_string::swap().
- Calling data() and c_str() member functions.
- Calling non-const member functions, except operator[](), at(),
  begin(), rbegin(),
  end(), and rend().
- 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().

The entry about non-const member functions seems to cover this. So unless I am missing something, then this code is using invalidated iterators right? Of course this code works just fine with gcc's libstdc++, but we all know that proves nothing as far as standards compliance.

+2  A: 

This will appear to work if replace operates in-place. I don't think it's required to be implemented that way though. So yes, I would say your code is technically illegal.

Kristo