vector and const
c++, constants, stl, vector
Solution
I've added a few lines to your code. That's sufficient to make it clear why this is disallowed:
void f(vector<const T*>& p)
{
static const T ct;
p.push_back(&ct); // adds a const T* to nonConstVec !
}
int main()
{
vector<T*> nonConstVec;
f(nonConstVec);
nonConstVec.back()->nonConstFunction();
}
Problem
Consider this ``` void f(vector<const T*>& p) { } int main() { vector<T*> nonConstVec; f(nonConstVec); } ``` The following does not compile.The thing is that `vector<T*>` can not be converted to `vector <const T*>` , and that seems illogically to me , because there exists implicit conversion from `T*` to `const T*`. Why is this ? `vector<const T*>` can not be converted to `vector <T*>` too, but that is expected because const `T*` can not be converted implicitly to `T*`.