Tech Off Post

Single Post Permalink

View Thread: Crash in std::vector (MSVC 2010 and MSVC 2012)
  • User profile image
    oyefremov

    Suggested implementation of emplace_back (for VS 2010 with one argument)

        template<class _Valty>
            void emplace_back(_Valty&& _Val)
            {    // insert element at end
                if (this->_Mylast == this->_Myend)
                {
                    size_type _Size = this->size();
                    if (max_size() - 1 < _Size)
                        _Xlen();
                    size_type _New_capacity = _Grow_to(_Size + 1);
    
                    _Myt _New_vector(this->_Alval);
                    _New_vector.reserve(_New_capacity);
    
                    _Cons_val(_New_vector._Alval, 
                        _New_vector._Myfirst + _Size,
                        _STD forward<_Valty>(_Val));
    
                    _TRY_BEGIN
                        // note, _Umove does not use allocator so it can not use _Cons_val (bug?)
                    _Umove(
                        this->_Myfirst, 
                        this->_Mylast, 
                        _New_vector._Myfirst);
                    _CATCH_ALL
                    _Dest_val(
                        _New_vector._Alval, 
                        _New_vector._Myfirst + _Size);
                    _RERAISE;
                    _CATCH_END
    
                    _New_vector._Mylast += _Size + 1;
                    std::swap(*this, _New_vector);
                    // _Orphan_range(this->_Mylast, this->_Mylast); Do we need it here???
                }
                else
                {
                    _Orphan_range(this->_Mylast, this->_Mylast);
                    _Cons_val(this->_Alval,
                        this->_Mylast,
                        _STD forward<_Valty>(_Val));
                    ++this->_Mylast;
                }
            }