当使用带有虚拟基类的boost序列化时,是什么原因导致C4250(类通过优势继承成员)?

时间:2010-11-03 20:03:39

标签: c++ visual-c++ boost boost-serialization typetraits

1 个答案:

答案 0 :(得分:0)

原因实际上是来自提升类型特征的is_virtual_base_of检查。如果检查成功,此检查构造将生成警告C4250,如此示例所示:

...
struct base { 
    virtual void mf() { };
};
struct derived_normal : public base { 
    virtual void mf() { };
};
struct derived_virt : virtual public base { 
    virtual void mf() { };
};

int main() {
    using namespace std;

    cout << "boost::is_virtual_base_of<base, derived_normal>::value reports: ";
    // The following line DOES NOT cause C4250
    cout << boost::is_virtual_base_of<base, derived_normal>::value << endl;

    cout << "boost::is_virtual_base_of<base, derived_virt> reports: ";
    // The following line causes C4250:
    cout << boost::is_virtual_base_of<base, derived_virt>::value << endl;
 ...

FWIW,这种类型特征工具在boost序列化中的用法如下:

  • macro BOOST_EXPORT_CLASS - &gt;
    • macro BOOST_CLASS_EXPORT_IMPLEMENT - &gt;
      • struct guid_initializer(在export.hpp中) - &gt;
      • (...)void_cast.hpp / void_cast_register - &gt; is_virtual_base_of在这里使用

据我所知,警告在这种情况下是完全无害的,可以通过将标题包装在:

来防止
#pragma warning( push )
#pragma warning( disable : 4250 ) // C4250 - 'class1' : inherits 'class2::member' via dominance
#include ...
#pragma warning( pop ) // C4250