In C++, when you evaluate a function, you end up with a value (unless the function's return type is void
). The type of a value is always an object type. So when you say f()
, that expression is a value of type T
. However, there are different categories of value:
T f(); => f() is a prvalue, passed along by copy
T & f(); => f() is an lvalue, the same object that is bound to "return"
T && f(); => f() is an xvalue, the same object that is bound to "return"
So if you want a function to produce an existing value that you don't want to copy, you have to declare the return type of the function as one of the reference types. If the return type is not a reference type, then a copy of the return value will be made, and the caller only ever sees that copy.