Take the following function (which obviously doesn't compile).
const std::vector<int>& getMyVector()
{
if (something)
return std::vector<int>();
if (somethingElse)
return m_myVector;
return std::vector<int>();
}
Assume that m_myVector is large or is otherwise not well suited to being returned by copy and so must be returned by reference.
What is the most efficient way to structure the code to allow this logic while also allowing a return by reference? I can see two options. The first would be to change this to return a pointer to the vector instead of a reference and then return null instead of an empty vector. The other would be to have a static const empty vector and return that. I don't particularly like either of those options, so I'm wondering if there's a better way.
Edit: Please don't use the example function as a literal representation of what I'm trying to illustrate. The point is that I have a function that can either return a real vector full of data or it can return a default empty vector based on certain conditions. Also, if it matters, my real code isn't even returning a simple member variable, it's a vector that's stored inside a hash_map.