Possible Duplicate:
In C++, is it still bad practice to return a vector from a function?
In terms of performance, when needing to return'heavier' objects like std::vector
or std::string
from a function, is it recommended to use this form:
void func(std::vector<int> *dest)
{
}
instead of this form:
std::vector<int> func()
{
std::vector<int> arr;
// ...
return arr;
}
I am assuming that the first form should be faster, but at the same time I've seen the second form often enough, the Qt API often returns a QString
for example, probably because it is much more convenient or intuitive to use.
Also I've wondered if there are compiler optimizations which could remove the unnecessary copying of objects when using the return statement.
Edit
Are there any popular compilers still used today which do not perform the optimizations mentioned in the answers?