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
Have you tried:
C++does not allow temporaries to be bound to non-const references.If you really to need to have a
vector<int>&(not aconstone), you can declare a static instance and use it as a default (thus non-temporary) value.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: