Please consider this code. I have seen this type of code several times. words
is a local vector. How is it possible to return it from a function? Can we guarantee it will not die?
std::vector<std::string> read_file(const std::string& path)
{
std::ifstream file("E:\\names.txt");
if (!file.is_open())
{
std::cerr << "Unable to open file" << "\n";
std::exit(-1);
}
std::vector<string> words;//this vector will be returned
std::string token;
while (std::getline(file, token, ','))
{
words.push_back(token);
}
return words;
}
I think you are referring to the problem in C (and C++) that returning an array from a function isn't allowed (or at least won't work as expected) - this is because the array return will (if you write it in the simple form) return a pointer to the actual array on the stack, which is then promptly removed when the function returns.
But in this case, it works, because the
std::vector
is a class, and classes, like structs, can (and will) be copied to the callers context. [Actually, most compilers will optimise out this particular type of copy using something called "Return Value Optimisation", specifically introduced to avoid copying large objects when they are returned from a function, but that's an optimisation, and from a programmers perspective, it will behave as if the assignment constructor was called for the object]As long as you don't return a pointer or a reference to something that is within the function returning, you are fine.
To well understand the behaviour, you can run this code :
The output is the following :
This is actually a failure of design, you shouldn't be using a return value for anything not a primitive for anything that is not relatively trivial. The ideal solution should be implemented through a return parameter with a decision on reference/ptr and the proper use of a "const\'y\'ness" as a descriptor.
On top of this, you should realise that the label on an array in C and C++ is effectively a pointer and its subscription are effectively an offset or an addition symbol.
So the label or ptr array_ptr === array label thus returning foo[offset] is really saying return element at memory ptr location foo + offset of type return type.
I do not agree and NOT RECOMMEND to return vector:
This is much faster:
I tested on VS2017 with following results in release mode:
8.01 MOPs by reference 5.09 MOPs returning vector
In debug mode things are much worse:
0.053 MOPS by reference 0.034 MOPs by return vector
As long there is no reference returned, it's perfectly fine to do so.
words
will be moved to the variable receiving the result.The local variable will go out of scope. after it was moved (or copied).
Pre C++11:
The function will not return the local variable but rather a copy of it. Your compiler might however perform an optimization where no actual copy action is made.
See this question & answer for further details
C++11:
The function will move the value, see this answer for further details