Dynamic_cast on llvm clang compiler not working

I see a strange failure when dynamic_cast returns NULL in the clang compiler. But the same code works with the gcc environment.

Could you tell me what could be the main reason? What is the difference between dynamic_cast on llvm and gcc.

I use the default behavior for both compilers, where, in my opinion, RTTI is enabled by default.

 template<typename T> T* find_msg_of_type( MsgList *list ) { T* msg = NULL; if (list) { for (std::vector<MsgList*>::iterator it = list->element.begin(); it != list->element.end(); it++) {// MsgList can be list of objects build with GSoap. if (typeid(*(*it)) == typeid(T)) { msg = dynamic_cast<T*>(*it); // Failing on clang but this same code is working with gcc compiler. break; } } } return msg; } 

Another observation: with gcc

 if (typeid(*(*it)) == typeid(T)) 

works fine as expected but with clang

 if (typeid(*(*it)) == typeid(T)) 

comparison shows different behavior. Not sure exactly why this is different.

thanks

+7
source share
1 answer

For such code, it is a good idea to statically ensure that the class T is derived from MsgList. Using boost, this can be done as follows:

BOOST_STATIC_ASSERT ((increase :: is_base_and_derived :: value));

0
source

All Articles