Czy jest zagwarantowane, że weak_ptr wygaśnie, gdy shared_ptr zostanie zresetowany na ten sam adres, który zawiera?Czy jest zagwarantowane, że weak_ptr wygaśnie, gdy shared_ptr zostanie zresetowany na ten sam adres, który zawiera?
#include <cassert>
#include <memory>
int main()
{
int* i = new int(0);
std::shared_ptr<int> si(i);
std::weak_ptr<int> wi = si;
si.reset(i);
assert(wi.expired()); // wi.expired() == true (GCC 4.7)
}
Czy jest tak w przypadku, gdy wartość wi.expired()
nie została zdefiniowana?
EDIT:
teraz modyfikować zapytania trochę:
To jest zagwarantowane, że weak_ptr
wygasa gdy shared_ptr
powraca do tego samego adresu, który zawierał shared_ptr
kiedy to został zainicjowany weak_ptr
?
#include <cassert>
#include <memory>
int main()
{
int* i = new int(0);
std::shared_ptr<int> si(i);
std::weak_ptr<int> wi = si;
si.reset();
int* j = new int(0);
// Suppose that new returns the same address that contains variable i :
assert(j == i);
si.reset(j);
assert(wi.expired()); // wi.expired() == true (GCC 4.7)
}
Powiązane pytanie: [Co stanie się po zresetowaniu std :: shared_ptr do siebie] (http: // stackoverflow.com/questions/9785656/what-happens-if-i-reset-a-stdshared-ptr-to-itself). – ComicSansMS