It seems that you confuse return parameter and input argument.
It's safe to pass argument by constant reference:
void fun(const vector<int>& arg1)
{
//something
}
In that example you pass only reference, so copy constructor isn't invoked.
But it's dangerous to return reference value (const or not const) from function.
See answer of Bruce above.
Also you can extend reference lifetime:
const vector<int>& Getv()
{
vector<int> w(10);
w[0]=10;
cout<<w.size()<<endl;
return w;
}
const vector<int>& v = Getv(); //now all ok
But I don't recomment this way. It can be source of future bugs.
The best way is returning value of vector.
vector<int> Getv()
{
vector<int> w(10);
w[0]=10;
cout<<w.size()<<endl;
return w;
}
vector<int>v = Getv();
Since there is only 1 return statement you can account for RVO and copy constructor won't be invoke too. it is fast and safe.