I’ve been trying to pass a std::vector to a function by reference with default value being an empty std::vector. The declaration of my function looks as follows:
void function( std::vector<double>& vec=std::vector<double>(0));
The definition of my function is:
void function( std::vector<double>& vec)
{
...
}
However my C++ compiler (gcc 4.6) is throwing an error here, saying:
error: default argument for parameter of type ‘std::vector&’ has type ‘std::vector’
I’ve seen this version of the code compile fine on a Microsoft VS 2010 compiler. And I’m wondering if this is an issue of different c++ standard interpretation between gcc and vs2010.
You can’t. You can’t bind a temporary to a ref-to-non-
const, so you could only do this:In your case, then, I suggest function overloading:
If you don’t like the extra function call then you’re out of luck. 🙂
I don’t know whether C++11 rvalue refs might help here, if you have access to them.
MSVS has a tendency to allow you to bind temporaries to refs-to-non-
const, which is non-standard behaviour. GCC is correct.