assertion_failed when using Boost Serialization with xml_oarchive
Asked Answered
O

3

6

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?

Onset answered 2/2, 2014 at 7:46 Comment(1)
Side note: BOOST_SERIALIZATION_NVP is a simple macro around boost::serialization::make_nvp(const char * name, T & t). You might not expose member names and use make_nvp("Number", Num) insteadSonorous
S
8

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);
Sonorous answered 2/2, 2014 at 8:23 Comment(2)
Oh, damn, I missed that part. I didn't realize I also had to use BOOST_SERIALIZATION_NVP for the entire object, but that does make sense. Thank-you!Onset
Side note: I did try traversing the Boost source code to find where the assertion failed, but I couldn't. My compiler error pointed me to a bunch of #defines and macro magic.Onset
S
0

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://mcmap.net/q/1709566/-assertion_failed-when-using-boost-serialization-with-xml_oarchive

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).

Sunnisunnite answered 9/5, 2016 at 2:22 Comment(0)
P
0

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.

Perfervid answered 18/8, 2016 at 15:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.