Heads-up: Changes to libc++'s <string> methods

Recently, “TC” reported a bug against libc++'s string implementation.
https://llvm.org/bugs/show_bug.cgi?id=25973

It seems that libc++'s string was not behaving correctly on calls to assign/replace/insert/append when exceptions were thrown - the string is required by the standard to remain unchanged in those cases (in some cases, libc++ would leave the string in a damaged, unusable state)

I have fixed this in: http://reviews.llvm.org/D15862 (not yet landed), but doing so requires some extra work. In particular, it requires constructing a temporary string and then moving the contents into the destination. (possible allocation, definitely copying).

For many iterators, these precautions are unnecessary, because they won’t throw exceptions. Libc++ attempts to recognize them and does the efficient thing in these cases. Pointers, for example. libc++'s vector/string iterators (when not using fancy pointers) are other examples.

There’s also a test for the general case. If the iterator operations that string uses are marked as noexcept, then you’ll get the (more) efficient code path. What are those operations?

  • dereference (i.e, *iter)
  • prefix increment (i.e, ++iter)
  • comparison ( iter1 == iter2 )
  • assignment ( iter1 = iter2 )

So, if you have custom iterators, and take a look at those operations and see if you can make them noexcept.

– Marshall