Is it always safe to pass an empty or uninitialised STL container to a function by reference? e.g.
void some_function(deque<string> &passed_by_ref) {
passed_by_ref.push_back("a string");
}
int main() {
deque<string> some_data;
some_function(some_data);
return 0;
}
I haven’t had any problems with this approach, but not sure if there could possibly be any NULL reference issues.
Yes, it’s always safe.
deque<T>is not a pointer type – it’s an object type. The standard containers have a default constructor, so, after this statementsome_datais a correctly constructed emptydeque.