Functions
F.49
Don't return `const T`
Reason
It is not recommended to return a const value. Such older advice is now obsolete; it does not add value, and it interferes with move semantics.
Example
const vector<int> fct(); // bad: that "const" is more trouble than it is worth
void g(vector<int>& vx)
{
// ...
fct() = vx; // prevented by the "const"
// ...
vx = fct(); // expensive copy: move semantics suppressed by the "const"
// ...
}
The argument for adding const to a return value is that it prevents (very rare) accidental access to a temporary. The argument against is that it prevents (very frequent) use of move semantics.
See also: F.20, the general item about "out" output values
Enforcement
- Flag returning a
constvalue. To fix: Removeconstto return a non-constvalue instead.