3

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?

2 Answers 2

3

1) It will do the same thing as if it wasn't const&. You can't modify const& parameters, 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 int by 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 like int by 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).

4
  • To be clear, you're not actually binding it to a literal - you're binding to a stack allocated memory location that happens to contain the value represented by the literal. You can modify it via const_cast<> in which case you're modifying the value stored in stack location (not harmful long term, unlike Fortran: ibiblio.org/pub/languages/fortran/ch1-8.html#01).
    – mcmcc
    Commented Jan 14, 2012 at 5:26
  • @mcmcc ah, I did not know that. Does it work that way for all compilers? Commented Jan 14, 2012 at 5:33
  • @mcmcc: Mutating a constant object via const_cast is undefined behaviour.
    – Kerrek SB
    Commented Jan 14, 2012 at 5:45
  • @Kerrek: There is no "constant object" -- there is a literal and a const reference to a temporary. As C++11's move constructors imply, it is perfectly allowable to modify a temporary.
    – mcmcc
    Commented Jan 23, 2012 at 16:15
2

References to const are special: they extend the lifetime of temporary objects.

Compare:

struct Foo { Foo(int, int) { } };

{
   Foo(1,2);                  // dies!

   const Foo & f = Foo(3,3);  // lives...

   // ...
}                             // ... now it's dead

In the same way, if you don't provide an integer, a temporary integer is constructed, and its lifetime is extend to the duration of the function call by virtue of being bound to parameter.

See Herb Sutter's GotW #88.

3
  • 1
    I don't think this has anything to do with his question does it? Commented Jan 14, 2012 at 5:05
  • @SethCarnegie: It answers "what exactly will it do when the default value is used?" I picked the interesting part of the question, I guess.
    – Kerrek SB
    Commented Jan 14, 2012 at 5:06
  • I didn't see anything about using const references with temporaries though. Unless 7 counts as a temporary, but I think it was just a coincidence. Commented Jan 14, 2012 at 5:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.