Warning C26815

The pointer is dangling because it points at a temporary instance that was destroyed. (ES.65)

Remarks

There's a dangling pointer that is the result of an unnamed temporary that has been destroyed.

Code analysis name: LIFETIME_LOCAL_USE_AFTER_FREE_TEMP

Example

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
}

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

See also

ES.65: Don't dereference an invalid pointer