Aracılığıyla paylaş


Uyarı C26815

İşaretçi, yok edilen geçici bir örneği işaret ettiğinden sallanıyor. (ES.65)

Açıklamalar

Oluşturulan işaretçi veya görünüm, deyiminin sonunda yok edilen adsız geçici bir nesneye başvurur. İşaretçi veya görünüm sallanır.

Bu denetim C++ Standart Şablon Kitaplığı'ndan (STL) görünümleri ve sahipleri tanır. Kullanıcı tarafından yazılan türler hakkında bu denetimi öğretmek için ek açıklamayı [[msvc::lifetimebound]] kullanın. Destek [[msvc::lifetimebound]] MSVC 17.7 sürümünde yenidir.

Kod analizi adı: LIFETIME_LOCAL_USE_AFTER_FREE_TEMP

Örnek

C++23'den önce C++ sürümünde derlenmiş aşağıdaki kodu göz önünde bulundurun:

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
}

Bu uyarılar geçici nesnenin ömrünü uzatarak düzeltilebilir.

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

Ayrıca bkz.

C26816
ES.65: Geçersiz işaretçi başvurusu kaldırma