Search code examples
c++stldeque

Relocation on assignment when using a std::deque


Does the C++ standard guarantee that when one pushes back a new element into a deque, none of the pre-existing elements will be relocated to a new memory address?


Solution

  • Yes, the standard provides such a guarantee:

    23.3.3.4 deque modifiers [lib.deque.modifiers]

    iterator insert(const_iterator position, const T& x);
    
    iterator insert(const_iterator position, T&& x);
    iterator insert(const_iterator position, size_type n, const T& x);
    template <class InputIterator>
    iterator insert(const_iterator position,
    InputIterator first, InputIterator last);
    iterator insert(const_iterator position, initializer_list<T>);
    template <class... Args> void emplace_front(Args&&... args);
    template <class... Args> void emplace_back(Args&&... args);
    template <class... Args> iterator emplace(const_iterator position, Args&&... args);
    void push_front(const T& x);
    void push_front(T&& x);
    void push_back(const T& x);
    void push_back(T&& x);
    

    1. Effects: An insert in the middle of the deque invalidates all the iterators and references to elements of the deque. An insert at either end of the deque invalidates all the iterators to the deque, but has no effect on the validity of references to elements of the deque.

    Absence of effect on references in fact mean that elements will not be relocated.

    According to the C++11 working draft N3242=11-0012