I have read this question "Why doesn't C++ support functions returning arrays?". It is said that when we attempt to access the array from outside of this function (via the return value), we have a problem because we are attempting to access memory that is not in the scope with which you are working (the function call's stack).
Doesn't the same problem happen when we return a std::string or std::vector which is declared inside the function or does C++ makes a copy of string or vector and returns the copy to caller so that the string or vector does not go out of scope.
vector<int> foo(const vector<int> a)
{
vector<int> b = a;
return b;
}
int main()
{
vector<int> a;
vector<int> c = foo(a);
}