refactor: Use std::erase and std::erase_if #36682

Manually merged
maflcko merged 2 commits from 2610-erase-if into master 2026-10-29 10:23:17 +00:00 AGit
maflcko commented 2026-10-28 09:12:27 +00:00 (Migrated from github.com)

Convert remove and remove_if to the C++20 std::erase and std::erase_if overloads.

The legacy ones can be confusing, because:

Also convert simple iterator-erase (which is verbose, but fine) to the std overload.

Convert `remove` and `remove_if` to the C++20 `std::erase` and `std::erase_if` overloads. The legacy ones can be confusing, because: * Array-based containers (like vector) must use the erase-remove idiom. Forgetting the erase, or forgetting to pass the end iterator (`bugprone-inaccurate-erase`) can be a bug, see also https://github.com/bitcoin/bitcoin/pull/36588#discussion_r4345799022. So a call only to "remove" will not erase. * Node-based containers (like list or set) must not use the idiom. Using it will be a pessimization or won't compile. And correctly not using the idiom reads confusing, because a call to "remove" will actually erase. * They are more verbose. Also convert simple iterator-erase (which is verbose, but fine) to the std overload.
DrahtBot commented 2026-10-28 09:12:38 +00:00 (Migrated from github.com)

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

External sites

Reviews

See the guideline and AI policy for information on the review process.

Type Reviewers
ACK hodlinator, jpk68, w0xlt
Concept ACK l0rinc

If your review is incorrectly listed, please copy-paste <!--meta-tag:bot-skip--> into the comment that the bot should ignore.

<!--e57a25ab6845829454e8d69fc972939a--> The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. <!--006a51241073e994b41acfe9ec718e94--> ### External sites * Code coverage & benchmarks: https://corecheck.dev/bitcoin/bitcoin/pulls/36682 * Ralph review <img src=https://review.fish.foo/assets/bitcoin/bitcoin/36682.png alt="Ralph review status"> https://git.fish.foo/bitcoin/bitcoin/pulls/36682#author-ralph <!--021abf342d371248e50ceaed478a90ca--> ### Reviews See [the guideline](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#code-review) and [AI policy](https://github.com/bitcoin/bitcoin/blob/master/doc/AI_POLICY.md) for information on the review process. | Type | Reviewers | | ---- | --------- | | ACK | [hodlinator](https://github.com/bitcoin/bitcoin/pull/36682#pullrequestreview-5680889758), [jpk68](https://github.com/bitcoin/bitcoin/pull/36682#issuecomment-6472445376), [w0xlt](https://github.com/bitcoin/bitcoin/pull/36682#pullrequestreview-5687288174) | | Concept ACK | [l0rinc](https://github.com/bitcoin/bitcoin/pull/36682#issuecomment-6468104394) | If your review is incorrectly listed, please copy-paste <code>&lt;!--meta-tag:bot-skip--&gt;</code> into the comment that the bot should ignore. <!--5faf32d7da4f0f540f40219e4f7537a3-->
First-time contributor

I found no actionable issues in this static review.


Base: 0f27bed5b292 · Head: fa87bfe31dfd · Full review report

<!-- forgejo-review-bot:bitcoin/bitcoin --> <!-- ralph-engine:current:aadde06fc908436af97c3ae22ac03220a75b46811daf747af5f90dd2d5b368be:58df985c6f760decb26503b27f8441ea78138ad8e02f110fa19b993630f18590 --> <!-- ralph-base:0f27bed5b2924420d65caeca439e6b036d3c6cb2 --> <!-- ralph-head:fa87bfe31dfda2d102c4e0cb5d39813cf84c7d1e --> <!-- ralph-case:66f77cb730af42b569a624aa3d4aa540a9af555e4558c673dd4ccf44a4917609 --> I found no actionable issues in this static review. <!-- Review tier: standard --> --- Base: [`0f27bed5b292`](<https://git.fish.foo/bitcoin/bitcoin/commit/0f27bed5b2924420d65caeca439e6b036d3c6cb2>) · Head: [`fa87bfe31dfd`](<https://git.fish.foo/bitcoin/bitcoin/commit/fa87bfe31dfda2d102c4e0cb5d39813cf84c7d1e>) · [Full review report](<https://review.fish.foo/traces/36682-fa87bfe31dfda2d102c4e0cb5d39813cf84c7d1e-1611-3.html>)
hodlinator (Migrated from github.com) approved these changes 2026-10-28 10:10:12 +00:00
hodlinator (Migrated from github.com) left a comment

ACK fa87bfe31d

The old erase()+remove() pattern is indeed error-prone, and translating the raw loops into named and shorter constructs is good.

ACK fa87bfe31dfda2d102c4e0cb5d39813cf84c7d1e The old `erase()`+`remove()` pattern is indeed error-prone, and translating the raw loops into named and shorter constructs is good.
Lines 166-167
hodlinator (Migrated from github.com) commented 2026-10-28 10:05:46 +00:00

nit: Have a slight preference for pre-existing approach when it's shorter. Although consistency is also nice.

nit: Have a slight preference for pre-existing approach when it's shorter. Although consistency is also nice.
maflcko (Migrated from github.com) reviewed 2026-10-28 10:17:49 +00:00
Lines 166-167
maflcko (Migrated from github.com) commented 2026-10-28 10:17:49 +00:00

Yeah, in C++-speak "remove" has two meanings: It means erase for std::list (unless you incorrectly use the iterator-based or range-based remove on the list, in which case it means move) and it means just move for array-based containers. I think consistently using "erase" to mean erase seems better than having to manually decipher what "remove" means in this context.

Yeah, in C++-speak "remove" has two meanings: It means erase for std::list (unless you incorrectly use the iterator-based or range-based remove on the list, in which case it means move) and it means just move for array-based containers. I think consistently using "erase" to mean erase seems better than having to manually decipher what "remove" means in this context.
l0rinc commented 2026-10-28 11:44:14 +00:00 (Migrated from github.com)

Concept ACK

Concept ACK
jpk68 commented 2026-10-28 14:58:47 +00:00 (Migrated from github.com)
ACK fa87bfe31dfda2d102c4e0cb5d39813cf84c7d1e
w0xlt (Migrated from github.com) reviewed 2026-10-28 17:54:45 +00:00
w0xlt (Migrated from github.com) left a comment
ACK fa87bfe31dfda2d102c4e0cb5d39813cf84c7d1e
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
bitcoin/bitcoin!36682
No description provided.