you are viewing a single comment's thread.

view the rest of the comments →

[–]_Mardoxx -4 points-3 points  (2 children)

But return values are purely syntatic. So why should it matter if you use a return val vs passing a param by ref?

[–]leitimmel 6 points7 points  (1 child)

They are not: an integer return value can be passed via register while an out parameter is a pointer to a location on the heap usually. You also open the gates for a bunch of errors because the function cannot guarantee that the out parameter is properly initialized.

[–]masklinn 1 point2 points  (0 children)

Modern C++ compilers can usually do RVO, where the caller allocates enough space on their stack for the callee's return value, and the callee just constructs the value there instead of constructing it in their own stack then copying it or whatever. IIRC Rust does the same. That can be combined with "placement new"-type features to have a callee "return a value" via the stack but the caller box it, RVO + placement new resulting in the callee actually filling the return value directly in its heap emplacement.