Search code examples
c++vectorconstantsreinterpret-cast

Is reinterpret cast from vector of pointers to vector of const pointers safe?


Is this conversion type safe

vector<int*> a;
const vector<const int*>& b = reinterpret_cast<const vector<const int*>&>(a);

A static cast obviously doesnt work in this case because the template argument is changing. But by doing this reinterpret cast, I am just adding constness to essentially the same type. So should this be safe for all practical purposes?


Solution

  • Like any use of reinterpret_cast to convert a reference to an unrelated type, it gives undefined behaviour, and so is not safe unless you define "safe" to mean "works on my compiler".