是否保证当 shared_ptr 重置为包含的相同地址时,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( i );
assert( wi.expired() ); // wi.expired() == true (GCC 4.7)
}
wi.expired()或者是没有定义值的情况?
编辑:
我现在稍微修改一下问题:
是否保证在重置为相同地址weak_ptr时会过期shared_ptr,该地址包含shared_ptr初始化时的地址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)
}