The type vector<char *>
is not convertible to const vector<const char*>
. For example, the following gives a compilation error:
#include <vector>
using namespace std;
void fn(const vector<const char*> cvcc)
{
}
int main()
{
vector<char *> vc = vector<char *>();
fn(vc);
}
I understand why vector<char*>
is not convertable to vector<const char*>
- extra members of type const char *
may be added to the vector, and afterwards they would be accessible as non-const. However, if the vector itself is const, this can't happen.
My best guess is that this would be harmless, but there is no way the compiler is allowed to deduce that this would be harmless.
How can this be worked around?
This question was suggested by the C++ FQA here.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…