Question

If I have a situation like the following, where a class B is derived from a class A in a different namespace, serialization oputput fails to validate tag name of base class producing "Invalid XML tag name".

How could I handle this situation?

#include <boost/archive/xml_oarchive.hpp>
#include <boost/serialization/base_object.hpp>

namespace N {

struct A
{
    friend class boost::serialization::access;
    int m_a;

    template<class Archive>
    void serialize(Archive & ar, const unsigned int version)
    {
        ar & BOOST_SERIALIZATION_NVP(m_a);
    }
};

}



struct B : N::A
{
    friend class boost::serialization::access;

    template<class Archive>
    void serialize(Archive & ar, const unsigned int version)
    {
        ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(N::A);
    }
};



int main ()
{
    B b;
    boost::archive::xml_oarchive ar(std::cout);
    ar << BOOST_SERIALIZATION_NVP(b);
}

the output is:

 <?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE boost_serialization>
<boost_serialization signature="serialization::archive" version="9">
<b class_id="0" tracking_level="0" version="0"terminate called after throwing an instance of 'boost::archive::xml_archive_exception'
  what():  Invalid XML tag name
The program has unexpectedly finished.
Was it helpful?

Solution

As you use different namespaces, BOOST_SERIALIZATION_BASE_OBJECT_NVP and BOOST_SERIALIZATION_NVP do not work. Instead, you need to use boost::serialization::make_nvp in both cases.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top