C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Proposal draft: unstable erase operations for std::vector

From: Adrian Johnston <ajohnston4536_at_[hidden]>
Date: Sun, 24 May 2026 15:22:29 -0700
Can I suggest using *erase_unordered()* instead of *erase_unstable()*? The
standard library already uses "unordered" to describe algorithms that do
not preserve ordering.

Also, if you are benchmarking code, can I suggest trying the *__restrict*
keyword as follows on arrays? It isn't part of the C++ standard but it is
something I would recommend an implementation of the C++ standard library
should be using. This code is written to avoid reloading registers at every
call to the callable when the compiler is unable to prove what the callable
does.

I can also make the case for an unordered erase call for a single index or
iterator.

template<hxarray_concept_ T_, size_t capacity_>
template<typename callable_t_>
size_t hxarray<T_, capacity_>::erase_if_unordered(callable_t_&& fn_) {
    size_t removed_ = 0u;
    T_* const begin_ = this->data();
    T_* __restrict end_ = m_end_;
    for(T_* __restrict it_ = end_; it_-- != begin_;) {
        if(hxforward<callable_t_>(fn_)(*it_)) {
            if(it_ != --end_) {
                *it_ = hxmove(*end_);
            }
            end_->T_::~T_();
            ++removed_;
        }
    }
    m_end_ = end_;
    return removed_;
}

Received on 2026-05-24 22:22:42