Advertencia C26815

El puntero es pendiente porque apunta a una instancia temporal que se destruyó. (ES.65)

Comentarios

El puntero o vista creado hace referencia a un objeto temporal sin nombre que se destruye al final de la instrucción. El puntero o la vista aparecerán.

Esta comprobación reconoce las vistas y los propietarios de la biblioteca de plantillas estándar (STL) de C++. Para enseñar esta comprobación sobre los tipos creados por el usuario, use la [[msvc::lifetimebound]] anotación . La [[msvc::lifetimebound]] compatibilidad es nueva en MSVC 17.7.

Nombre del análisis de código: LIFETIME_LOCAL_USE_AFTER_FREE_TEMP

Ejemplo

Tenga en cuenta el código siguiente compilado en una versión de C++ antes de C++23:

std::optional<std::vector<int>> getTempOptVec();

void loop() {
    // Oops, the std::optional value returned by getTempOptVec gets deleted
    // because there is no reference to it.
    for (auto i : *getTempOptVec()) // warning C26815
    {
        // do something interesting
    }
}

void views()
{
    // Oops, the 's' suffix turns the string literal into a temporary std::string.
    std::string_view value("This is a std::string"s); // warning C26815
}

struct Y { int& get() [[msvc::lifetimebound]]; };
void f() {
    int& r = Y{}.get(); // warning C26815
}

Estas advertencias se pueden corregir ampliando la duración del objeto temporal.

std::optional<std::vector<int>> getTempOptVec();

void loop() {
    // Fixed by extending the lifetime of the std::optional value by giving it a name.
    auto temp = getTempOptVec();
    for (auto i : *temp)
    {
        // do something interesting
    }
}

void views()
{
    // Fixed by changing to a constant string literal.
    std::string_view value("This is a string literal");
}

struct Y { int& get() [[msvc::lifetimebound]]; };
void f() {
    Y y{};
    int& r = y.get();
}

Consulte también

C26816
ES.65: No desreferenciar un puntero no válido