|
Boost Users : |
From: Richard Jennings (Richard.Jennings_at_[hidden])
Date: 2005-08-09 07:59:35
I have found that the requirement that no serialization headers can be
included before any archive headers means that you have to be careful
which serialization headers you include in your class headers and indeed
in what order you include them.
In particular if you include serialization/nvp.hpp before
serialization/vector.hpp and you use that class in another module that
does not use archiving at all then that module will hit the error
directive, example below.
A solution would seem to be that serialization/nvp.hpp should only be
included in the module that includes archive headers.
I guess what I would like is some guidance on using serialization as
it's become a bit more tricky in my experience. At least you can't
willy-nilly include serialization headers in any order in any headers
and expect the application to compile. Perhaps it only worked by luck
before!
For example, given class A below:
class A
{
private:
friend class boost::serialization::access;
template <class ArchiveT>
void serialize(ArchiveT &ar, unsigned int)
{
ar & BOOST_SERIALIZATION_NVP(m_Vector);
}
std::vector<int> m_Vector;
};
What serialization headers should be included in A's header? What
should be included in the header of the class that serializes A?
My inclination would be to include serialization/nvp.hpp and
serialization/vector.hpp since these are needed if (but only if) the
template is instantiated. This is on the idea that a class should
(generally) compile on it's own and you should not have to look to see
what it is composed of to be able to get it to compile.
Here's the example that does not work:
B fails to compile with
[C++ Fatal Error] basic_archive.hpp(21): F1003 Error directive: "no
serialization headers my precede any archive headers"
If you include nvp after vector, then it's ok too!
A.h:
=======
#include <vector>
#include <boost/serialization/nvp.hpp>
#include <boost/serialization/access.hpp>
#include <boost/serialization/vector.hpp>
class A
{
private:
friend class boost::serialization::access;
template <class ArchiveT>
void serialize(ArchiveT &ar, unsigned int)
{
ar & BOOST_SERIALIZATION_NVP(m_Vector);
}
std::vector<int> m_Vector;
};
=======
B.h:
class B
{
public:
B();
};
=======
B.cpp:
#include "B.h"
#include "A.h"
B::B()
{
A InstanceOfA;
}
=======
Main.cpp
#include <sstream>
#include <boost/archive/text_oarchive.hpp>
#include "A.h"
#include "B.h"
int main(int argc, char* argv[])
{
A InstanceOfA;
B InstanceOfB;
std::stringstream ss;
boost::archive::text_oarchive oa(ss);
oa & InstanceOfA;
return 0;
}
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net