The first example should rather look like one of the following:
void getLines(std::string in, std::vector<std::string> &out); void getLines(std::string in, std::vector<std::string> *out);
The second style is often more convenient and theoretically can be done basically as effectively as the first:
http://en.wikipedia.org/wiki/Return_value_optimization
As far as this is seen in practice, it depends on the compiler, optimization levels and whether the compiler can detect this feature in the first place.
Until I speak for everyone, I have never managed to get compilers to use RVO, even with simple objects that do nothing. Therefore, I therefore personally recommend the first approach, because you are guaranteed to get the desired behavior on each compiler and (which is important to me ...) for each programmer code, i.e. So that the object created to save the return value is filled directly with the function being called without any additional copies.
(The cost of creating a default result object that could be avoided was the RVO to be used, usually low compared to a copy that could not be avoided by the compiler.)
Another point that should be noted is that if you try to avoid unnecessary copies, passing objects via a const reference instead of a value is often a good idea, for example:
void getLines(const std::string &in, std::vector<std::string> &out); void getLines(const std::string &in, std::vector<std::string> *out);
please delete me
source share