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:
uns
The only case where it is valid to return by const reference is if the object you are returning will outlive the function call* (such as returning a member of the class on which the function has been invoked), and even in that context, whether one should do that is dubious, since that will allow two different callers to access the same memory location, which makes things a nightmare to fix if you use multiple threads.
*NOTE: In your case, the item you are returning is a local variable and therefore will not outlive the function call. Hence the code you have provided is invoking the nefarious undefined behavior.