https://gcc.gnu.org/bugzilla/show_bug.cgi?id=119745

--- Comment #3 from GCC Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Tomasz Kaminski <[email protected]>:

https://gcc.gnu.org/g:716ec14c573c7ec9e3732e66c7288db8f0348154

commit r16-7532-g716ec14c573c7ec9e3732e66c7288db8f0348154
Author: Tomasz KamiÅski <[email protected]>
Date:   Mon Feb 16 10:18:34 2026 +0100

    libstdc++: Implement rvalue overload for basic_string::substr() [PR119745]

    This paper implement the changes from P2438R2 basic_string::substr() &&
    paper into C++26. The additional substr and constructor overload are
    implemented only for SSO string, as they require mutating the content
    (if reused), and thus would require copy of the string anyway for COW
    strings. (In consequence allocators implicitly constructible from int
    remain ambiguous for C++11 ABI strings, see r16-7497-gfa1149534d8580).

    In addition to cases when the allocators are not compatible (equal),
    this patch does not reuse (transfer) allocator storage, if the selected
    substring fits inside the SSO buffer, so we do not risk keeping large
    chunk of memory for few characters. (This also covers cases when the
    source stored the content in the local buffer).

    As this additional overloads are meant to be optimization, in contrast
    to move constructor, the source is left unmodified if the allocation
    is not transferred. This avoid introducing a write (of null terminator)
    to previously untouched, heap allocated, memory.

    Separate overloads for substr(size_type __pos, size_type __n) and
    substr(size_type __pos == 0), that delegate to corresponding constructor,
    are provided to avoid the check of __n against the length() in the later
    case.

    Finally, the signatures of existing substr() overload are not modified
    (no longer required since C++20), which avoid any impact on the ABI.

            PR libstdc++/119745

    libstdc++-v3/ChangeLog:

            * include/bits/basic_string.h (basic_string::_M_construct)
            [__cplusplus >= 202302L]: Declare.
            (basic_string::basic_string(basic_string&&, size_type, const
_Alloc&))
            (basic_string(basic_string&&, size_type, size_type, const _Alloc&))
            (basic_string::substr(size_type, size_type) &&)
            (basic_string::substr(size_type) &&) [__cplusplus >= 202302L]:
Define.
            * include/bits/basic_string.tcc (basic_string::_M_construct)
            [__cplusplus >= 202302L]: Define.
            * testsuite/21_strings/basic_string/operations/substr/rvalue.cc:
New test.

    Reviewed-by: Jonathan Wakely <[email protected]>
    Reviewed-by: Patrick Palka <[email protected]>
    Signed-off-by: Tomasz KamiÅski <[email protected]>

Reply via email to