Reputation: 503
I have a function,
void test( vector<int>& vec );
How can I set the default argument for vec ? I have tried
void test( vector<int>& vec = vector<int>() );
But there's a warning "nonstandard extension used : 'default argument' : conversion from 'std::vector<_Ty>' to 'std::vector<_Ty> &'"
Is there a better way to do this ? Instead of
void test() {
vector<int> dummy;
test( dummy );
}
Regards, Voteforpedro
Upvotes: 50
Views: 67141
Reputation: 96
I know this question was asked almost 10 years ago. But for anyone finding its solution now, I've got exactly that for you. I basically have three different methods:
void test(vector<int>vec = vector<int>(10, 0){//definition}
This can be used if it has to be if the default vector has a given size with a common initialiser value.
void test(vector<int>vec = vector<int>{1, 2, 3, 4}){//definition}
This can be used if it has to be if the default vector has a given size with a different initialiser values.
A different approach could be to define a global vector object as:
vector<int> globalvector;
(initialised or uninitialised) and the passing this object as default value as
void test(vector<int>vec = globalvector){//definition}
An important benefit of the last one is that it gives more control over the default value itself as functions can be used to assign values to this globalvector
before control reaches the test()
function. Another benefit is that (as far I've yet found), if the function involves passing by reference, this works just as fine as:
void test(vector<int>& vec = globalvector){//definition}
unlike the former two.
Upvotes: 0
Reputation: 71
You could just make pass a pointer to a vector and initialize it to NULL
void test( vector<int>* vec = NULL );
Upvotes: 7
Reputation: 343
I know this question was asked in 2010 but for C++11 you can do it like this:
void test( vector<int>& vec = {}) {
// Awesome code here
}
As pointed out in this answer.
Upvotes: 5
Reputation: 55776
Have you tried:
void test(const vector<int>& vec = vector<int>());
C++
does not allow temporaries to be bound to non-const references.
If you really to need to have a vector<int>&
(not a const
one), you can declare a static instance and use it as a default (thus non-temporary) value.
static vector<int> DEFAULT_VECTOR;
void test(vector<int>& vec = DEFAULT_VECTOR);
But beware, because DEFAULT_VECTOR will (can) be modified and won't reset on each call ! Not sure that this is what you really want.
Thanks to stinky472, here is a thread-safe alternative:
Instead of providing a default value, you might as well overload test()
with a zero-parameter version which calls the other version:
void test()
{
vector<int> vec;
test(vec);
}
Upvotes: 79
Reputation: 224129
I find it questionable for a non-const
reference argument to have a default value. What is this supposed to mean?
Commonly, a function taking a non-const
reference means "I might change the argument". If the argument is optional why no pass a (non-const
) pointer? That way, when callers don't want to pass an arguments, they can pass NULL
.
(See here for more information on how to pass function arguments.)
Edit: Well, and of course, there's also overloading. Just add another overload which doesn't take this argument.
Upvotes: 9
Reputation: 6797
We cannot initialize mutable references to temporaries. Only const references allow this. What you are after is most likely this:
void test( const vector<int>& vec = vector<int>() );
Aside from avoiding undefined behavior, this makes the most sense logically and from a const-correctness perspective. If you wanted 'test' to modify the original vector being passed to it, you would not have been able to sensibly provide a default value. Thus it's obvious you are using 'vec' here for read-only purposes and should therefore make it a const reference.
Upvotes: 4