Advertencia C26815 El puntero está colgando porque apunta a una instancia temporal que se destruyó.Warning C26815 The pointer is dangling because it points at a temporary instance that was destroyed. (ES.65)(ES.65)

Hay un puntero colgante que es el resultado de un 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");
}

Consulte tambiénSee also

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