libc ++ vector :: insert overloads

时间:2015-06-07 16:23:19

标签: c++ c++11 vector libc++

libc ++的std::vector implementation具有insert的以下重载:

template <class _Tp, class _Allocator>
typename vector<_Tp, _Allocator>::iterator
vector<_Tp, _Allocator>::insert(const_iterator __position, const_reference __x)
{
    pointer __p = this->__begin_ + (__position - begin());
    if (this->__end_ < this->__end_cap())
    {
        __RAII_IncreaseAnnotator __annotator(*this);
        if (__p == this->__end_)
        {
            __alloc_traits::construct(this->__alloc(),
                                      _VSTD::__to_raw_pointer(this->__end_), __x);
            ++this->__end_;
        }
        else
        {
            __move_range(__p, this->__end_, __p + 1);
            const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
            if (__p <= __xr && __xr < this->__end_) // [*]
                ++__xr;
            *__p = *__xr;
        }
        __annotator.__done();
    }
    else
    {
        allocator_type& __a = this->__alloc();
        __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
        __v.push_back(__x);
        __p = __swap_out_circular_buffer(__v, __p);
    }
    return __make_iter(__p);
}

......和类似的,采用右值参考:

template <class _Tp, class _Allocator>
typename vector<_Tp, _Allocator>::iterator
vector<_Tp, _Allocator>::insert(const_iterator __position, value_type&& __x)
{
    pointer __p = this->__begin_ + (__position - begin());
    if (this->__end_ < this->__end_cap())
    {
        __RAII_IncreaseAnnotator __annotator(*this);
        if (__p == this->__end_)
        {
            __alloc_traits::construct(this->__alloc(),
                                      _VSTD::__to_raw_pointer(this->__end_),
                                      _VSTD::move(__x));
            ++this->__end_;
        }
        else
        {
            __move_range(__p, this->__end_, __p + 1);
            *__p = _VSTD::move(__x);
        }
        __annotator.__done();
    }
    else
    {
        allocator_type& __a = this->__alloc();
        __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
        __v.push_back(_VSTD::move(__x));
        __p = __swap_out_circular_buffer(__v, __p);
    }
    return __make_iter(__p);
}

第一次重载时标有[*]的分支的用途是什么?它是否需要标准?为什么在第二次超载时不存在?我无法在libstdc++中找到等效的构造。

通过创建临时副本,

修改libstdc++ solves同样的问题。

1 个答案:

答案 0 :(得分:8)

这是处理您尝试插入的元素已存在于vector中的情况。

为了解释这一点,让我们从定义函数中使用的变量开始。

  • __p是指向要插入新元素的位置的指针
  • __xr是指向要插入的元素的地址的指针

vector具有足够的容量来插入其他元素(if (this->__end_ < this->__end_cap()))时,将执行您询问的代码路径。此外,插入点不是end()迭代器(if (__p == this->__end_) - 执行else路径)。

在这种情况下,实现首先将[__p, end())范围内的所有内容进一步移动 - __move_range(__p, this->__end_, __p + 1);

但是,如果您尝试插入的元素是刚刚移动的范围的一部分,该怎么办?如果是这样,则必须将指针递增到要插入的元素。这就是以下几行

if (__p <= __xr && __xr < this->__end_)
  ++__xr;

rvalue引用重载没有进行相同的检查,因为允许实现假设rvalue引用引用的任何对象都是唯一引用的,因此尝试执行带有rvalue引用的insert vector中已存在的元素是未定义的行为。

来自N3337,§17.6.4.9/ 1 [res.on.arguments]

  

除非另有明确说明,否则以下各项均适用于C ++标准库中定义的函数的所有参数    - ......
   - 如果函数参数绑定到右值引用参数,则实现可能会假定此参数是对此参数的唯一引用。

以下是上述条款的defect report和基本原理。

相关问题