Where to put BOOST_CLASS_EXPORT for boost::serialization?

蹲街弑〆低调 提交于 2019-12-03 02:23:08

Exporting Class Serialization of the Boost.Serialization docs (1.44.0) states the following:


BOOST_CLASS_EXPORT in the same source module that includes any of the archive class headers will instantiate code [...]

Note that the implemenation of this functionality requires that the BOOST_CLASS_EXPORT macro appear after and the inclusion of any archive class headers for which code is to be instantiated. So, code that uses BOOST_CLASS_EXPORT will look like the following:

#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
... // other archives

#include "a.hpp" // header declaration for class a
BOOST_CLASS_EXPORT(a)
... // other class headers and exports

[...] Including BOOST_CLASS_EXPORT in the "a.hpp" header itself as one would do with other serialization traits will make it difficult or impossible to follow the rule above regarding inclusion of archive headers before BOOST_CLASS_EXPORT is invoked. This can best be addressed by using BOOST_CLASS_EXPORT_KEY in the header declarations and BOOST_CLASS_EXPORT_IMPLEMENT in the class definition file.


I ended up putting all the serialization code in a header s11n.h that is included from the CPP file that invokes the serialization. Essentially, the EXPORT_IN_MAIN scenario I sketched above, but with the BOOST_CLASS_EXPORT macro invocations in a different file.

This only works as long as only one compilation unit includes s11n.h, of course, so although it works for now, it's no real solution...

You can use EXPORT_IN_OBJECT but the file that contains BOOST_CLASS_EXPORT must also include all the archive hpp files that plan to use.

This is because the BOOST_CLASS_EXPORT macro registers the derived type information which each archive it thinks you will use (implicitly determined based upon which archives you have included.)

In your example, use EXPORT_IN_OBJECT but also add #include to export.cpp.

In our code, we created archives.hpp that contains the archives we use and include it where ever we need to use BOOST_CLASS_EXPORT. (That way we have a single official list of archives.)

The downside is that we need to rebuild everything when we decide to use a new archive type, but we found that much easier to use than the polymorphic archive support.

you can use and unique BOOST_CLASS_EXPORT_GUID() for each .cpp and add it only in the .cpp. not the .h

This problem drove me insane until I realized that my base class was not polymorphic. In other words, it never used the keyword "virtual" anywhere. Because I didn't need polymorphic behavior.

Here is how I fixed it:

  1. I just slapped the keyword "virtual" on some random method in my base class.
  2. In my derived class's .cpp file, I added the following:

    #include <boost/serialization/export.hpp>
    BOOST_CLASS_EXPORT(testnamespace::derivedclass)
    

This is all that I had to do.

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