VS2010 swap()操作引起的问题

来源:互联网 发布:无法连接到一个windows 编辑:程序博客网 时间:2024/05/26 19:14

最近遇到一个VS2010的问题。
debug跟到代码里,才注意到有这个_Move() 之类的函数。

我遇到的问题是由于map::swap()引起的。

之前vs2005可以支持swap发生在不同的allocator下,现在会产生问题,估计是为了支持"右值引用"导致的。


虽然从vs2010的代码看起来,应该是继续提供支持的,放点代码过来:

 

void swap(_Myt& _Right)
  {  // exchange contents with _Right
     if (this == &_Right)
     ; // same object, do nothing
      else if (get_allocator() == _Right.get_allocator())
     { // same allocator, swap control information
      this->_Swap_all(_Right);
      _Swap_adl(this->comp, _Right.comp);
      _STD swap(this->_Myhead, _Right._Myhead);
      _STD swap(this->_Mysize, _Right._Mysize);
     }
    else
    { // different allocator, do multiple assigns
      _Myt _Tmp = _Move(*this);

      *this = _Move(_Right);
      _Right = _Move(_Tmp);
     }
    }

 

但是,由于这个_Move() 会间接调到_Assign_rv() 就会导致问题!

void _Assign_rv(_Myt&& _Right)
  {  // assign by moving _Right
     if (this == &_Right)
      ;
     else if (get_allocator() != _Right.get_allocator())
       _Xinvalid_argument("invalid map/set<T> move assign");
    else
    { // clear this and steal from _Right
      clear();
      this->_Swap_all(_Right);
      _Swap_adl(this->comp, _Right.comp);
      _STD swap(this->_Myhead, _Right._Myhead);
      _STD swap(this->_Mysize, _Right._Mysize);
     }
   }

原创粉丝点击