We cannot be 100% shared because different platforms have different ABIs, but I think we can make some fairly general statements that will be applied in most implementations with the caution that these things mainly apply to functions that are not included.
First, consider primitive types. At a low level, the pass by reference parameter is implemented using a pointer, while primitive return values are usually passed literally to registers. Therefore, return values are more likely to work better. On some architectures, this applies to small structures. Copying a value small enough to fit in a register or two is very cheap.
Now consider larger, but still simple (default constructors, copy constructors, etc.). Usually, larger return values are handled by passing the function a pointer to the place where the return value should be given. Copying elision allows you to return a variable from a function, a temporary one, used to return, and a variable in the caller that the result is placed in a unit. Thus, the basics of transmission will be the same for transmission by reference and return.
In general, for primitive types, I would expect the return values to be slightly better for larger, but still simple types, I would expect them to be the same or better if your compiler is not very bad at copying.
For types using default constructors, copy constructors, etc. things get more complicated. If the function is called several times, then the return values will force the object to be rebuilt each time, while the reference parameters may allow the reuse of the data structure without being restored. On the other hand, the reference parameters will force the (possibly unnecessary) construct before calling the function.
plugwash Nov 30 '15 at 13:36 2015-11-30 13:36
source share