Const reference parameter to a fundamental scalar type
ID |
c.correctness.const_reference_to_scalar |
Severity |
low |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Api Design |
Language |
C / C++ |
Description
This parameter takes a fundamental scalar type by const reference. A scalar is at most as large as a pointer and cheap to copy, so binding it by reference adds a layer of indirection the caller has to pay for on every access without saving anything — it can even be slower than passing by value once the reference is dereferenced repeatedly. Pass the parameter by value instead.
Rationale
This parameter takes a fundamental scalar type by const reference. A scalar is at most as large as a pointer and cheap to copy, so binding it by reference adds a layer of indirection the caller has to pay for on every access without saving anything — it can even be slower than passing by value once the reference is dereferenced repeatedly. Pass the parameter by value instead.
The following code illustrates the pattern detected by this rule:
// FLAGGED: Const reference parameter to a fundamental scalar type
void setSpeed(const int &speed)
{
(void)speed;
}