c++ - Pass non-const vector<string> to a function that takes a reference to const vector<const string>? -


i have function want able accept const vector<const string> want user able pass vector<string> well. thought make function argument reference const , non-const vector still accepted, it's not. here's example:

void test(const vector<const string> &v) {     return; }  int main( int argc, char *argv[] ) {     vector<string> v;     test(v);      return 0; } 

the error receive is:

error c2664: 'test' : cannot convert parameter 1 'std::vector<_ty>' 'const std::vector<_ty> &' 


why doesn't example work , how suggest make function work user can pass const vector<const string> , vector<string>? thanks

this post maybe can you. can force call in (horrible) way

test(*reinterpret_cast<vector<const string>*>(&v)); 

that in way illustrates why not possible automatic conversion. break container's rules:

vector<string> v = {"a", "b"}; vector<const string>& vc = v; //not allowed, of course v[0].clear(); //<-- vc[0] cleared 

Comments

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -