template <class T> class Foo {};
class Manager {
std::unordered_map<size_t, std::unique_ptr<std::vector<void *>>> _mFoos;
public:
template <class T> void addFoo(Foo<T> &foo) {
int x = typeid(T).hash_code();
// Safe ? :
auto &p = *(reinterpret_cast<std::unique_ptr<std::vector<Foo<T>>> *>(&_mFoos[x]));
if (p == nullptr) {
p.reset(new std::vector<Foo<T>>);
}
auto &foos = *p;
foos.push_back(foo);
};
template <class T> Foo<T> &getFoo(int index) {
int x = typeid(T).hash_code();
auto it = _mFoos.find(x);
auto &foos = *(reinterpret_cast<std::vector<Foo<T>> *>(it->second.get()));
return foos[index];
};
};
Is it safe and portable to reinterpret_cast a unique_ptr< T > in a unique_ptr< T2 > if T and T2 are two different types ? Does they have the same size and the same bit pattern ?