Let's say I have two functions. One returns std::vector<int>
and another returns a class that has a std::vector<int>
member.
// using only the vector
std::vector<int> buildVector(){
std::vector<int> value;
// build the vector...
return value;
};
// using and returning a class instead
struct Something{
std::vector<int> m_vector;
}
Something buildSomething(){
Something value;
// build the object...
return value;
};
Will the two functions buildVector()
and buildSomething()
perform shallow copies of the objects they're returning (avoiding an unnecessary heap allocation)? Or will they not? Or would they behave differently?
I assume the internal pointers of the vector will remain valid, unless I'm mistaken and the local variable decides to deallocate this memory. (will they?)
I'm not after performance per se, but I don't want to perform allocations that are not needed. And writing the functions in this form feels the most natural for what I'm doing.
My compiler doesn't use C++11; would this be a different scenario in the current standard compared with the prior standard?