Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Pass By Const Reference and Return By Const Reference

I'm trying to understand if there is any benefit to returning a const reference. I have a factorial function that normally looks like this:

unsigned long factorial(unsigned long n)
{
    return (n == 0) ? 1 : n * factorial(n - 1);
}

I'm assuming that there will be a performance increase when we pass by const reference and we return a const reference... but const-correctness always confuses me.

const unsigned long & factorial(const unsigned long& n)
{
    return (n == 0) ? 1 : n * factorial(n - 1);
}

Is it valid to return a const reference? Furthermore, could somebody please tell me: is it beneficial?

like image 210
Kiril Avatar asked Jul 09 '10 22:07

Kiril


People also ask

What is the difference between pass by reference and pass by const reference?

From what I understand: when you pass by value, the function makes a local copy of the passed argument and uses that; when the function ends, it goes out of scope. When you pass by const reference, the function uses a reference to the passed argument that can't be modified.

Does C support pass by constant reference?

C does not support references or passing by reference. You should use pointers instead and pass by address. Pass-by-value is efficient for primitive types, but does a shallow copy for structs. In C++ it makes a LOT of sense to pass objects by reference for efficiency.

What is passing by const reference?

Passing By Reference To Const in C++ | QuantStart. Passing By Reference To Const in C++ Passing By Reference To Const in C++ C++ is an example of a message-passing paradigm language, which means that objects and values are passed to functions, which then return further objects and values based on the input data.

Can a const function return a reference?

If the thing you are returning by reference is logically part of your this object, independent of whether it is physically embedded within your this object, then a const method needs to return by const reference or by value, but not by non-const reference.


3 Answers

This is invalid. You can't return reference to a local variable.

MSVS C++ compiler even gives the following warning:

main.cc : warning C4172: returning address of local variable or temporary

Not quite sure about GCC, but probably the result would be the same.

like image 60
M. Williams Avatar answered Oct 25 '22 13:10

M. Williams


A const reference isn't faster than a value, if the size of the value is small. In this case the type of the value is long, which is IMO small (e.g. 4 to 8 bytes): so a const reference will be no faster. In fact it may be slower, because to get the value of a reference the compiler may need to emit the code that will dereference the reference (like dereferencing a pointer).

Given that a reference is implemented (internally) like a pointer, I'd expect to get better performance from passing references than from passing values when the size of the value is bigger than the size of a pointer (assuming that it's even legal to pass a reference: a reference to a local variable that's gone out of scope isn't legal).

like image 33
ChrisW Avatar answered Oct 25 '22 14:10

ChrisW


The const reference is incorrect here - you're returning a reference to a local variable - an un-named temporary here, either 1 or the result of n * factorial(n - 1). Because the reference is to a local variable in the function, by the time the reference gets to the caller, that local variable has already gone out of scope, and is invalid.

Return a const reference to large structured types that you want to avoid a copy to when the reference will survive the exit of the function. Usually, this means returning a reference to an argument or to a member variable (in the case of classes).

like image 27
Thanatos Avatar answered Oct 25 '22 12:10

Thanatos