Issue 56031
Summary [libc+] std::basic_string::insert pessimization for custom character types
Labels
Assignees
Reporter treh
    std::basic_string::insert unnecessarily always copies into a `__temp` buffer if `charT` is a custom type.

[The implementation](https://github.com/llvm/llvm-project/blob/c36870c8e79c1690432076c69cb98a879555efa8/libcxx/include/string#L2930-L2949) uses [`__string_is_trivial_iterator`](https://github.com/llvm/llvm-project/blob/c36870c8e79c1690432076c69cb98a879555efa8/libcxx/include/string#L604-L613) to determine if it should copy the input range into a `basic_string __temp` buffer, before calling `__insert_from_safe_copy`.

However, for custom class-type `_CharT`, even a plain `_CharT const*` does not satisfy `__string_is_trivial_iterator`, so nothing is won by creating this buffer.

Shouldn't the definition for `__string_is_trivial_iterator` use something like `is_nothrow_convertible<Tp, _CharT>` instead of `is_arithmetic<Tp>`?

Note: Custom class-type character types **are** allowed in `basic_string`, the [requirements are](https://timsong-cpp.github.io/cppwp/n4861/strings#basic.string-1)

> The class template basic_­string describes objects that can store a sequence consisting of a varying number of arbitrary char-like objects with the first element of the sequence at position zero. Such a sequence is also called a “string” if the type of the char-like objects that it holds is clear from context. In the rest of this Clause, the **type of the char-like objects** held in a basic_­string object is designated by **charT**.

and *char-like* is defined [before](https://timsong-cpp.github.io/cppwp/n4861/strings#general-1) as

> This Clause describes components for manipulating sequences of **any non-array trivial standard-layout type. Such types are called char-like types**, and objects of char-like types are called char-like objects or simply characters.

Note2: This even was a bug from [this]( https://reviews.llvm.org/D98573) to [this](https://reviews.llvm.org/D119633) revision, causing infinite recursion.

Here is a small example to play around with: [main.cpp](https://github.com/llvm/llvm-project/files/8899669/main.cpp.txt)

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to