assertion_failed when using Boost Serialization with xml_oarchive

六月ゝ 毕业季﹏ 提交于 2019-12-07 06:24:38

问题


When compiling a simple test of Boost Serialization:

class Test
{
protected:
    int Num;

    friend class boost::serialization::access;
    template <class Archive>
    void serialize(Archive & ar, const unsigned int version)
    {
        ar & BOOST_SERIALIZATION_NVP(Num);
    }

public:
    Test(): Num(0) {}
    ~Test() {}
};

using an xml_oarchive for output, I am experiencing the following GCC error:

C:\Development\Libraries\boost_1_55_0\boost\mpl\assert.hpp|289|error: no matching function for call to 'assertion_failed(mpl_::failed************ boost::serialization::is_wrapper<Test>::************)'|

When using other oarchive types, it compiles and runs fine. All the research I have done pointed me to using the BOOST_SERIALIZATION_NVP macro to solve the error, but I am already doing that and I still get this error.

Has anyone experienced this same issue?


回答1:


As described in the source code of boost serialization:

// Anything not an attribute and not a name-value pair is an
// error and should be trapped here.
template<class T>
void save_override(T & t, BOOST_PFTO int)
{
    // If your program fails to compile here, its most likely due to
    // not specifying an nvp wrapper around the variable to
    // be serialized.
    BOOST_MPL_ASSERT((serialization::is_wrapper< T >));
    this->detail_common_oarchive::save_override(t, 0);
}

You likely do not use it with your Test object:

Test test;
ar << BOOST_SERIALIZATION_NVP(test);



回答2:


I get the boost::assertion_failed undefined error when I forget to include our source defining this in a new project.

Independent of anything else, you should define boost::assertion_failed as described at http://www.boost.org/doc/libs/1_55_0/libs/utility/assert.html (version matching OP's dated question, but similar in the latest) and in the answer at https://stackoverflow.com/a/21508521/527489

As stated in the docs, "The user is expected to supply an appropriate definition."

Defining this might make compiler errors somewhat less frustrating (likely still very frustrating though).




回答3:


I'd like to add that the documentation for Boost.Serialization actually explicitly says something about this, because it seems nobody has mentioned it.

Your code failed to compile only when using an xml_oarchive and not other oarchive types. This is boost's official explanation.

From http://www.boost.org/doc/libs/1_45_0/libs/serialization/doc/wrappers.html#nvp
(emphasis mine)

XML archives present a somewhat special case. XML format has a nested structure that maps well to the "recursive class member visitor" pattern used by the serialization system. However, XML differs from other formats in that it requires a name for each class data member. Our goal is to add this information to the class serialization specification while still permiting the the serialization code to be used with any archive.



来源:https://stackoverflow.com/questions/21508256/assertion-failed-when-using-boost-serialization-with-xml-oarchive

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!