Those aren't equivalent. Form 1 takes no inputs but has one
output, but vice versa for form 2. If you meant to pass the
string by non-const reference, then I prefer the former,
since:
- the latter requires the string to be non-const in the
caller, and
- the latter can only be used for assignment, not
initialization, whereas the former can be used for either.
As an extension to what you've said: you can't use the second in
expressions. Things like getCurrentPath() + '/', for example.
Which is, IMHO, a killer argument.
It's worth noting, too, that C++ has absolutely no support for
out parameters. For in parameters, you can use pass by value or
const reference. For inout, pass by non const reference, but
for out... You're still required to provide an in value, even
if the called function doesn't care.
There's not necessarily any copy in form 1, either. The
compiler is allowed to elide the copy. Form 2 can actually
prevent some optimizations, since GetCurrentPath might cache a
pointer to the caller's string.
Not to mention the fact that you've got to construct a string
before calling the function. Constructing a string immediately,
to return it, could be quicker constructing it, then assigning a
value to it.
But of course, the argument is misplaced. It's hard to imagine
a case where the difference would be measurable, and when such a
case occurs, that's the time to fix it. Speculatively fixing
supposed performance problems before they exist is just stupid.