Does returning by reference always ensure no copy constructor is called?
Yes. When you pass or return by reference (as opposed to the passing/returning by value), no copy is being constructed.
is RVO taking place here?
Since you return by reference, RVO has nothing to do with it here. RVO is an optimization technique that consists in elimination of a redundant copy being created while returning by value.
RVO might take a place if you have function like this:
Type GetType()
{
Type instance;
//...
return instance;
}
//...
Type t = GetType();
then with RVO the compiler would try to eliminate the redundant copy constructor and destructor calls which means that the local instance
from the GetType
function would be assigned into variable t
without a copy being created.