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

Abierto
#6,502 9 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 2 días

@frederick-vs-ja ya está trabajando en esto.

Desde el 23/11/2026.

Evaluación

Dificultad
3/5
Tiempo estimado
1-2 días
Aptitud para principiantes
57/100
Tipo de issue
Error
Claridad
Bastante claro
Estado de actividad
Activo
Stack tecnológico
cpp
Área
backend

Línea de trabajo

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.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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

Lenguaje dominante
C++
Estrellas
11.2k
Forks
1.7k
Merge medio
4 d 4 h
PR fusionados (30 d)
14

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de microsoft/STL

Todos los issues de microsoft/STL

Issues similares

Más issues de C++

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.