Usually, parameter values are copied when being passed on. Using a reference may save memory, especially for big structs. However, in this case:
void foo( int parameter = 7 );
void bar( const int& parameter = 7 );
in the second declaration, what exactly will it do when the default value is used? What happens when a constant value is used for a reference parameter?
void foo( const int& parameter ) { }
...
foo( 7 );
is there any difference in efficiency between a reference and a copied value when a contant value is used?
1) It will do the same thing as if it wasn’t
const&. You can’t modifyconst¶meters, so you’re OK to bind them to constants1.2) There might be a very tiny difference in efficiency because you’re really (probably, implementation dependent) passing a pointer to the value instead of the value, so you’ll have to dereference the pointer to get to the real value. (Then again, the compiler could see that passing an
intby const reference is a waste of time so it will pass the thing by value and pretend to you that it’s by const reference. You can’t know.) This sacrifice may be worth it for larger constructs, but generally you don’t ever need to pass something likeintby const reference.1 mcmcc pointed out that in the case of integer literals, you are really binding the the reference to a location on the stack (unlike string literals, for instance, which reside in static memory).