Improper erase-remove idiom
Container's erase()
is not called or called improperly following
a call to std::remove()
Since R2022a
Description
This defect occurs when any of these conditions are true:
The function
std::remove()
orstd::remove_if()
is called and the output is not passed to a container'serase()
method.The container's
erase()
method is called by using the output ofstd::remove
but without passing the second parameter.
Risk
You might expect std::remove()
or std::remove_if
to remove entries from a container. Instead, these functions partition a container and move
the entries that match the removal criteria to the right side. This right side content might
not be meaningful. If you do not pass the output of std::remove()
and
std::remove_if
to the container's erase()
method,
the entries are not removed and the container might remain in an indeterminate state.
When you call a container's erase()
by using the output of
std::remove
but do not specify the second parameter, the call to
erase()
might lead to unexpected or undefined behavior. For instance,
such a call to erase()
removes only the first entry on the right side of
the container instead of removing all of them. If std::remove()
returns
the end()
iterator, such a malformed erase()
results
in undefined behavior.
Fix
To remove entries from a container, use these two steps:
First, call
std::remove()
orstd::remove_if
to move the entries that you want to remove to the right side of the container.Then, call the container's
erase()
method to remove the entries on the right side of the container.
Avoid using std::remove()
without a following call to
erase()
. Some containers implement their own
remove()
methods. For these containers, it might be sufficient to use
these remove()
methods instead of
std::remove()
.
If you want to partition a container, use std::partition()
.
If you use C++20, use the functions std:erase()
and
std::erase_if()
. These functions implement the erase-remove idiom in a
single call.
When calling the containers erase()
with the output of
std::remove()
, specify the second parameter of
erase()
to avoid undefined behaviors. Generally, the second parameter
of erase()
matches the second parameter of
std::remove()
.
Examples
Result Information
Group: Programming |
Language: C++ |
Default: Off |
Command-Line Syntax:
STD_REMOVE_WITHOUT_ERASE |
Impact: Medium |
Version History
Introduced in R2022a
See Also
Topics
- Interpret Bug Finder Results in Polyspace Desktop User Interface
- Interpret Bug Finder Results in Polyspace Access Web Interface (Polyspace Access)
- Address Results in Polyspace User Interface Through Bug Fixes or Justifications
- Address Results in Polyspace Access Through Bug Fixes or Justifications (Polyspace Access)