Basically I’ve a class A and a class B : public A.
And I’d like to cast a std::shared_ptr<std::vector<A*> to a std::shared_ptr<std::vector<B*>
The problem is std::vector<B> doesn’t inherit from std::vector<A>, and the smart_ptr neither. So I do a horrible cast:
std::shared_ptr<VectorA> vector_a = * ((std::shared_ptr<VectorA>*)&vector_b);
The code compiles and runs there, but it is safe?
http://liveworkspace.org/code/3dQTz1$0
Strictly speaking the dereferencing operations should succeed. Both vectors are pointer containers, so casting one to another, whilst unacceptable for production code, will still use the same dimensions and alignment.
C++ provides rich abstractions to avoid these shenanigans though, it would be better to populate the vector of derived objects as pointers to the base class.