Date: Sat, 23 May 2026 10:20:36 +0800
Hello,
I would like to propose a small standard library extension for std::vector:
std::unstable_erase and std::unstable_erase_if.
The motivation is to standardize the common swap-and-pop idiom used when
removing elements from a vector where the relative order of the remaining
elements is not semantically important.
C++20 provides std::erase and std::erase_if for standard containers. For
std::vector, these operations preserve the relative order of the remaining
elements. In performance-sensitive code, such as games, simulations, servers,
and dense object storage, preserving order can impose unnecessary relocation
costs.
The proposed interface is:
template<class T, class Alloc, class U>
constexpr typename std::vector<T, Alloc>::size_type
unstable_erase(std::vector<T, Alloc>& c, const U& value);
template<class T, class Alloc, class Pred>
constexpr typename std::vector<T, Alloc>::size_type
unstable_erase_if(std::vector<T, Alloc>& c, Pred pred);
The operation removes matching elements but does not preserve the order of
the remaining elements. It is intended as an unstable counterpart to
std::erase / std::erase_if for std::vector, not as a replacement for them.
A public draft paper, prototype implementation, and preliminary benchmark
results are available here:
https://github.com/BaiJin0224/unstable-erase
I would appreciate feedback on the following points:
1. Whether this is a reasonable container-level facility for std::vector.
2. Whether the names unstable_erase and unstable_erase_if are appropriate.
3. Whether the initial scope should remain limited to std::vector.
4. Whether the wording should specify a swap-and-pop-like complexity model.
5. How exception safety should be specified.
Thank you,
Xisheng Liu
I would like to propose a small standard library extension for std::vector:
std::unstable_erase and std::unstable_erase_if.
The motivation is to standardize the common swap-and-pop idiom used when
removing elements from a vector where the relative order of the remaining
elements is not semantically important.
C++20 provides std::erase and std::erase_if for standard containers. For
std::vector, these operations preserve the relative order of the remaining
elements. In performance-sensitive code, such as games, simulations, servers,
and dense object storage, preserving order can impose unnecessary relocation
costs.
The proposed interface is:
template<class T, class Alloc, class U>
constexpr typename std::vector<T, Alloc>::size_type
unstable_erase(std::vector<T, Alloc>& c, const U& value);
template<class T, class Alloc, class Pred>
constexpr typename std::vector<T, Alloc>::size_type
unstable_erase_if(std::vector<T, Alloc>& c, Pred pred);
The operation removes matching elements but does not preserve the order of
the remaining elements. It is intended as an unstable counterpart to
std::erase / std::erase_if for std::vector, not as a replacement for them.
A public draft paper, prototype implementation, and preliminary benchmark
results are available here:
https://github.com/BaiJin0224/unstable-erase
I would appreciate feedback on the following points:
1. Whether this is a reasonable container-level facility for std::vector.
2. Whether the names unstable_erase and unstable_erase_if are appropriate.
3. Whether the initial scope should remain limited to std::vector.
4. Whether the wording should specify a swap-and-pop-like complexity model.
5. How exception safety should be specified.
Thank you,
Xisheng Liu
Received on 2026-05-23 02:20:46
