[cfe-dev] Heads-up: Changes to libc++'s <string> methods

Marshall Clow via cfe-dev cfe-dev at lists.llvm.org
Thu Jan 7 09:35:30 PST 2016


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20160107/159a6ef2/attachment.html>


More information about the cfe-dev mailing list