llvm clang编译器上的dynamic_cast失败 [英] dynamic_cast on llvm clang compiler failing

查看:145
本文介绍了llvm clang编译器上的dynamic_cast失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到一个奇怪的故障,其中dynamic_cast在clang编译器上返回NULL.但是相同的代码也可以在gcc环境下工作.

I am seeing a strange failure where the dynamic_cast is returning NULL on clang compiler. But the same code is working with gcc environment.

能否请您指出可能是根本原因? llvm上的dynamic_cast和gcc有什么区别.

Could you please point me what might be the root cause? What might the difference between the dynamic_cast on llvm and gcc.

我正在使用两个编译器的默认行为,我认为默认情况下启用了RTTI.

I am using the default behavior of both the compiler where i think the RTTI is enable 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;
}

另一个观察结果:使用gcc

One more observation: With gcc

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

运行正常,但与叮当声

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

比较显示出不同的行为..不确定为什么会有所不同.

comparison is showing different behavior.. not sure exactly why this differs.

谢谢

推荐答案

对于这样的代码,一个好主意是静态地确保类T是从MsgList派生的.使用boost可以这样完成:

For code like this, a Good Idea is to statically ensure that the class T is derived from MsgList. Using boost, this can be done thusly:

BOOST_STATIC_ASSERT((boost :: is_base_and_derived :: value));

BOOST_STATIC_ASSERT((boost::is_base_and_derived::value));

这篇关于llvm clang编译器上的dynamic_cast失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆