Why shared pointer assignment does 'swap'?
问题 As i understand when assigned shared ptr should behave like: a) if (--this->count == 0) { release this->count and this->obj } b) this->count = r->count, this->obj = r->obj; What boost does is just shared_ptr & operator=( shared_ptr const & r ) BOOST_NOEXCEPT { this_type(r).swap(*this); return *this; } So what is the magic behind swap and why this works? 回答1: a) if (--this->count == 0) { release this->count and this->obj } No, a shared_ptr keeps two counts, one for the object and one for the