#include <iostream>
#include <typeinfo>
using namespace std;
template<typename T>
class A
{
public: T x;
virtual void v(){};
};
int main() {
A<int> a1;
A<float> a2;
cout<<typeid(a1).name()<<endl;
cout<<typeid(a2).name()<<endl;
void * pv = static_cast<void*>(&a2);
A<int> * pai = static_cast<A<int>*>(pv);
cout<<typeid(pai).name()<<endl; //pointer to A<int>
cout<<typeid(*pai).name()<<endl; //A<float>!
A<float> *dynamic = dynamic_cast<A<float>*>(pai); //funny, donno what happens here
if(dynamic != nullptr) {
cout<<typeid(*dynamic).name()<<endl;
} else {
cout<<"dynamic_cast failed!"<<endl;
}
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dHlwZWluZm8+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7Cgp0ZW1wbGF0ZTx0eXBlbmFtZSBUPgpjbGFzcyBBCnsKCXB1YmxpYzogVCB4OwoJdmlydHVhbCB2b2lkIHYoKXt9Owp9OwoKCmludCBtYWluKCkgewoJCglBPGludD4gYTE7CglBPGZsb2F0PiBhMjsKCWNvdXQ8PHR5cGVpZChhMSkubmFtZSgpPDxlbmRsOwoJY291dDw8dHlwZWlkKGEyKS5uYW1lKCk8PGVuZGw7Cgl2b2lkICogcHYgPSBzdGF0aWNfY2FzdDx2b2lkKj4oJmEyKTsKCUE8aW50PiAqIHBhaSA9IHN0YXRpY19jYXN0PEE8aW50Pio+KHB2KTsKCQoJY291dDw8dHlwZWlkKHBhaSkubmFtZSgpPDxlbmRsOyAvL3BvaW50ZXIgdG8gQTxpbnQ+Cgljb3V0PDx0eXBlaWQoKnBhaSkubmFtZSgpPDxlbmRsOyAvL0E8ZmxvYXQ+IQoJQTxmbG9hdD4gKmR5bmFtaWMgPSBkeW5hbWljX2Nhc3Q8QTxmbG9hdD4qPihwYWkpOyAvL2Z1bm55LCBkb25ubyB3aGF0IGhhcHBlbnMgaGVyZQoJaWYoZHluYW1pYyAhPSBudWxscHRyKSB7CgkJY291dDw8dHlwZWlkKCpkeW5hbWljKS5uYW1lKCk8PGVuZGw7Cgl9IGVsc2UgewoJCWNvdXQ8PCJkeW5hbWljX2Nhc3QgZmFpbGVkISI8PGVuZGw7Cgl9CglyZXR1cm4gMDsKfQ==