Message: Parameter 'x' can be declared as reference to const
Category: Code Quality
Severity: Style
Language: C++
A reference parameter is never used to modify what it refers to, so it could be declared as a reference
to const.
A missing const hides a guarantee the compiler could otherwise enforce and readers could otherwise
rely on: that the function only reads through the reference, never modifies the caller's object through
it.
Before:
#include <vector>
auto foo(std::vector<int>& vec, bool flag) { // <- 'vec' is only read
std::vector<int> dummy;
std::vector<int>::iterator iter;
if (flag)
iter = vec.begin();
else {
dummy.push_back(42);
iter = dummy.begin();
}
return *iter;
}After:
#include <vector>
auto foo(const std::vector<int>& vec, bool flag) {
std::vector<int> dummy;
std::vector<int>::iterator iter;
if (flag)
iter = dummy.begin();
else {
dummy.push_back(42);
iter = dummy.begin();
}
return *iter;
}- constParameter.md - the array-parameter equivalent.
- constParameterPointer.md - the pointer-parameter equivalent.
- constVariableReference.md - the same idea, for a local reference variable instead of a parameter.
- constParameterCallback.md - the same idea, but for a parameter of a function used as a callback.