Is there any benefit (or conversely, cost) to passing a parameter by const value in the function signature?
So:
void foo( size_t nValue )
{
// ...
vs
void foo( const size_t nValue )
{
// ...
The only reason for doing this that I can think of would be to ensure the parameter wasn’t modified as part of the function although since it has not been passed by reference there would be no wider impact outside the function.
Of course there is. You can’t modify
nValueinside the function.As with
const-ness in general, it’s not a security measure, since you can cast it away, but a design matter.You’re explicitly telling other programmers that see your code –
and programmers that maintain or change your code