Repository: bitcoin/bitcoin
File: src/prevector.h (C/C++)

Code before:
        return iterator(ptr);
    }

    void insert(iterator pos, size_type count, const T& value) {
        size_type p = pos - begin();
        size_type new_size = size() + count;
        if (capacity() < new_size) {
            change_capacity(new_size + (new_size >> 1));
        }
        T* ptr = item_ptr(p);
        memmove(ptr + count, ptr, (size() - p) * sizeof(T));
        _size += count;
        fill(item_ptr(p), count, value);
    }

    template <std::input_iterator InputIterator>
    void insert(iterator pos, InputIterator first, InputIterator last) {
        size_type p = pos - begin();
        difference_type count = last - first;
        size_type new_size = size() + count;
        if (capacity() < new_size) {
            change_capacity(new_size + (new_size >> 1));
        }
        T* ptr = item_ptr(p);
        memmove(ptr + count, ptr, (size() - p) * sizeof(T));
        _size += count;
        fill(ptr, first, last);
    }

    inline void resize_uninitialized(size_type new_size) {
        // resize_uninitialized changes the size of the prevector but does not initialize it.
        // If size < new_size, the added elements must be initialized explicitly.
        if (capacity() < new_size) {
            change_capacity(new_size);
            _size += new_size - size();
            return;
        }
        if (new_size < size()) {
            erase(item_ptr(new_size), end());
        } else {
            _size += new_size - size();
        }
    }

    iterator erase(iterator pos) {
        return erase(pos, pos + 1);
    }

    iterator erase(iterator first, iterator last) {
        // Erase is not allowed to the change the object's capacity. That means
        // that when starting with an indirectly allocated prevector with

Review comment (refactor):
In commit "refactor: Make code more tolerant of constexpr std::arrays" (c4c102de7336e6c0cc2e5187feedf437ebadb565)

Sorry I didn't read the previous threads which might explain this, but this is a very vague comment which should be improved. It's is not clear looking at the new code what "temporary expression" or "explicit type" the comment is referring to, what "calms down GCC" could mean or what "some circumstances" could be. Would suggest writing a more understandable comment like `// Passing dst to memmove instead of (ptr + count) prevents GCC error/warning "<whatever the error/warning is>"`


Code after:
        new(static_cast<void*>(ptr)) T(value);
        return iterator(ptr);
    }

    void insert(iterator pos, size_type count, const T& value) {
        size_type p = pos - begin();
        size_type new_size = size() + count;
        if (capacity() < new_size) {
            change_capacity(new_size + (new_size >> 1));
        }
        T* ptr = item_ptr(p);
        T* dst = ptr + count;
        memmove(dst, ptr, (size() - p) * sizeof(T));
        _size += count;
        fill(item_ptr(p), count, value);
    }

    template <std::input_iterator InputIterator>
    void insert(iterator pos, InputIterator first, InputIterator last) {
        size_type p = pos - begin();
        difference_type count = last - first;
        size_type new_size = size() + count;
        if (capacity() < new_size) {
            change_capacity(new_size + (new_size >> 1));
        }
        T* ptr = item_ptr(p);
        T* dst = ptr + count;
        memmove(dst, ptr, (size() - p) * sizeof(T));
        _size += count;
        fill(ptr, first, last);
    }

    inline void resize_uninitialized(size_type new_size) {
        // resize_uninitialized changes the size of the prevector but does not initialize it.
        // If size < new_size, the added elements must be initialized explicitly.
        if (capacity() < new_size) {
            change_capacity(new_size);
            _size += new_size - size();
            return;
        }
        if (new_size < size()) {
            erase(item_ptr(new_size), end());
        } else {
            _size += new_size - size();
        }
    }

    iterator erase(iterator pos) {
        return erase(pos, pos + 1);
    }