This rule leads to greater precision in the definition of local variables by making the developer intention about modifying the variable explicit.
The const
qualification shall be applied to the object pointed to, not to the pointer, since it is the object itself that is being
protected.
Noncompliant code example
std::string& getString();
void myfunc()
{
std::string& s = getString(); // Noncompliant
if (s.size()) {
std::cout << s;
}
}
Compliant solution
std::string& getString();
void myfunc () {
const std::string& x = getString();
if (s.size()) {
std::cout << s;
}
}