Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 319
user1353535 Avatar asked Sep 05 '12 11:09

user1353535


1 Answers

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".

like image 115
Mike Seymour Avatar answered Oct 23 '22 12:10

Mike Seymour