ADVERTENCIA C26815 el puntero está pendiente porque apunta a una instancia temporal que se ha destruido.Warning C26815 The pointer is dangling because it points at a temporary instance that was destroyed. (ES. 65)(ES.65)

Hay un puntero pendiente que es el resultado de una temporal sin nombre que se ha destruido.There is a dangling pointer that is the result of an unnamed temporary that has been destroyed.

EjemploExample

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
}

Estas advertencias se pueden corregir extendiendo la duración del objeto temporal.These warnings can be fixed by extending the lifetime of the temporary object.

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");
}

Consulta tambiénSee also

ES. 65: no desreferenciar un puntero no válidoES.65: Don't dereference an invalid pointer