With pointers?
I can think of two ways to meet OP's requirements (using an empty vector - but this could be an int as well - as a default and modifying it in the function):
Code:
void foo(std::vector<int>* vec = new std::vector<int>())
{
//use and modify *vec
}
void bar(std::vector<int>* vec = 0)
{
bool is_null = !vec;
if (is_null)
vec = new std::vector<int>();
//use and modify *vec
if (is_null)
delete vec;
}
Now, the first one is elegant, but seems to create an unavoidable memory leak as there is no way of telling whether a vector pointer was passed or not.
The second one is ugly but hopefully correct.
Given these options I would drop the requirement of a default parameter altogether:
Code:
void foo(std::vector<int>& vec);
//calling with an empty "default"
std::vector<int> empty_vec;
foo(empty_vec);
(The idea with a function overload is the same: if you call the overload that takes no arguments, that would create the empty vector for you and call the main version that takes a vector and does the real work.)