[libcxx-commits] [libcxx] [libc++] Use char_traits::copy while inserting when possible (PR #97201)

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Thu Jul 4 08:22:15 PDT 2024


ldionne wrote:

The optimization makes sense to me, however `string::insert` says:

> Inserts characters from the range `[first, last)` before the element (if any) pointed by `pos`, as if by `insert(pos - begin(), basic_string(first, last, get_allocator()))`

I read this as saying that it's fine to insert a substring of the string into the string. Something like this:

```
std::string s = "0123456789";
auto first = s.begin();
auto last = s.end();
s.insert(s.begin() + 3, first, last);
assert(s == "01201234567893456789");
```

Godbolt: https://godbolt.org/z/33znr65oT

So I don't think we can perform this optimization, at least not without checking for overlap in the input range first.

https://github.com/llvm/llvm-project/pull/97201


More information about the libcxx-commits mailing list