rspec/rules/S5294/cfamily/rule.adoc

27 lines
732 B
Plaintext
Raw Normal View History

== Why is this an issue?
2021-04-28 16:49:39 +02:00
Container member functions that modify the containers often take an iterator as input to specify a precise location work on. If this iterator comes from a different container than the one calling the function, the result will be undefined.
=== Noncompliant code example
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,cpp]
2021-04-28 16:49:39 +02:00
----
void test(std::vector &v1, std::vector &v2) {
v1.insert(v2.begin(), v2.begin(), v2.end()); // Noncompliant, the first argument is the insertion location and must be in v1
v1.erase(v2.begin()); // Noncompliant
}
----
=== Compliant solution
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,cpp]
2021-04-28 16:49:39 +02:00
----
void test(std::vector &v1, std::vector &v2) {
v1.insert(v1.begin(), v2.begin(), v2.end()); // Compliant
v1.erase(v1.begin()); // Compliant
}
----