`<string>`: `basic_string`'s erasing operations don't orphan iterators

Aperta
#6,502 9 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 2 giorni

@frederick-vs-ja ci sta già lavorando.

Dal 23/11/2026.

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
57/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
cpp
Ambito
backend

Direzione di ricerca

Start by comparing basic_string iterator handling for pop_back, erase, and clear with vector's behavior in the MSVC STL. Use the command-line test case with _ITERATOR_DEBUG_LEVEL == 2 and -std:c++20 -D_DEBUG to check the reported gap. Done means erasing operations diagnose invalidated iterators in both compile-time and run-time debug checks, as described.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

bug

Describe the bug

As discovered by Copilot in https://github.com/microsoft/STL/pull/6500#discussion_r4377205703, MSVC STL's basic_string seems to lack iterator orphaning in erasing operations (pop_back, erase, and clear).

It's unclear to me whether this is by design. At least the status of basic_string is inconsistent with vector.

Command-line test case

#include <cassert>
#include <string>
#include <vector>

#if _ITERATOR_DEBUG_LEVEL != 2
#error
#endif // _ITERATOR_DEBUG_LEVEL != 2

constexpr bool test_string() {
    auto s = std::string{"The quick brown fox jumps over the lazy dog"};
    auto it = s.cbegin();
    auto jt = s.cbegin() + 10;
    s.erase(it + 5);

    return it == jt - 10;
}

static_assert(test_string()); // not diagnosed when _ITERATOR_DEBUG_LEVEL == 2

constexpr bool test_vector() {
    auto v = std::vector{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
    auto it = v.cbegin();
    auto jt = v.cbegin() + 10;
    v.erase(it + 5);

    return it == jt - 10;
}

static_assert(test_vector()); // diagnosed when _ITERATOR_DEBUG_LEVEL == 2

Godbolt link, using -std:c++20 -D_DEBUG.

Expected behavior

test_string() also fails in debug modes, in both compile time and run time.

STL version

Latest https://github.com/microsoft/STL/commit/b6f01e438b68ee497f9c08313e49d12bb2f0d8e0.

(For the online example, probably https://github.com/microsoft/STL/commit/edd1486e5fe753616b6fd3695da96f352b4092d2.)

Additional context

Lingua principale
C++
Stelle
11.2k
Fork
1.7k
Merge medio
4g 4h
PR unite (30g)
14

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di microsoft/STL

Tutte le issue di microsoft/STL

Issue simili

Altre issue su C++

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.