std::weak_ptr::operator=混乱
std::weak_ptr::operator= confusion
在查看 std::weak_ptr::operator=
on cppreference 时,我意识到我不明白重载 (1-3) 的解释是什么意思。也就是说,它表示
template< class Y >
weak_ptr& operator=( const shared_ptr<Y>& r ) noexcept;
等同于
std::weak_ptr<T>(r).swap(*this)
怎么可能? std::weak_ptr<T>(r)
不是右值(临时值), .swap(*this)
指的是它的实例,即与自身交换吗?我原以为应该是
std::weak_ptr<T>().swap(r)
这里到底发生了什么?
std::weak_ptr<T>(r)
创建一个临时 weak_ptr
指向与 r
相同的对象;
.swap(*this)
将临时对象的值与当前实例交换,使当前实例指向与r
相同的对象,而临时对象指向当前实例曾经指向的任何对象;
;
销毁临时对象,因此现在少了一个 weak_ptr
指向当前实例过去指向的对象。
在查看 std::weak_ptr::operator=
on cppreference 时,我意识到我不明白重载 (1-3) 的解释是什么意思。也就是说,它表示
template< class Y >
weak_ptr& operator=( const shared_ptr<Y>& r ) noexcept;
等同于
std::weak_ptr<T>(r).swap(*this)
怎么可能? std::weak_ptr<T>(r)
不是右值(临时值), .swap(*this)
指的是它的实例,即与自身交换吗?我原以为应该是
std::weak_ptr<T>().swap(r)
这里到底发生了什么?
std::weak_ptr<T>(r)
创建一个临时 weak_ptr
指向与 r
相同的对象;
.swap(*this)
将临时对象的值与当前实例交换,使当前实例指向与r
相同的对象,而临时对象指向当前实例曾经指向的任何对象;
;
销毁临时对象,因此现在少了一个 weak_ptr
指向当前实例过去指向的对象。