MISRAC++2023-9.5.2 (C++ only)
In this section:
Synopsis
(Required) A for-range-initializer shall contain at most one function call
Enabled by default
Yes
Severity/Certainty
Medium/High

Full description
Doing multiple calls may cause undefined behaviour regarding temporary object lifetime.
Coding standards
This check does not correspond to any coding standard rules.
Code examples
The following code example fails the check and will give a warning:
#include <vector>
#include <string>
extern std::vector < std::string > make();
void example() {
for ( char c: make().at(0) ) {} // Non-compliant - two function calls
}
The following code example passes the check and will not give a warning about this issue:
#include <vector>
#include <string>
extern std::vector < std::string > make();
void example() {
auto v = make();
for ( char c: v.at(0) ) {} // Compliant
}