Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

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

Non-Compliant Example

Compliant Example

The relationship between size and capacity makes it possible to predict when a call to a non-const member function will cause a string to perform a reallocation. This in turn makes it possible to predice when an insertion will invalidate references, pointers, and iterators (to anything other than the end of the string).

In the following example, the call to push_back() does not invalidate the iterator.

Code Block

string s;
...
if ( s.size() < s.capacity() ) {
  s.push_back('x');
}

If instead of performing a push_back(), the code were to insert into an arbitrary location in the string, all references, pointers, and iterators from the insertion point to the end of the string are invalidated.

Exceptions

The intent of these iterator invalidation rules is to give implementors greater freedom in implementation techniques. Some implementations implement method version that do not invalidate references, pointers, and iterators in all cases. Check with your implementation specific documentation and document any violation of the semantics specified by the standard for portability.

...