Hi folks,
I stumbled over a strange behavior in the serialization.
Basically I want to write multiple entries into the same archive. The
data is a map of an index and some shared_ptr for the values.
Writing/reading are working but produce wrong results with the provided
minimal example.
I'm using boost 1.49 under Visual Studio 2010 64bit.
I've also tried the code without the use of shared_ptr and without a
map. Both version are working fine. The combination however fails.
What am I doing wrong?
cheers
Sebastian
<snip>
#include <map>
#include <fstream>
#include
#include
#include
#include
#include
#include
struct Test
{
template <class Archive>
void serialize(Archive& ar, const unsigned int /*version*/)
{
ar & c;
}
Test()
:c(0)
{}
int c;
};
typedef std::map TestMap;
void save(const TestMap& m)
{
static std::ofstream os("d:/tmp/somedata.bin", std::ios_base::binary);
static boost::archive::binary_oarchive oa(os);
oa << m;
}
void load(TestMap& m)
{
static std::ifstream is("d:/tmp/somedata.bin", std::ios_base::binary);
static boost::archive::binary_iarchive ia(is);
ia >> m;
}
int main(int argc, char** argv)
{
TestMap t;
t.insert(std::make_pair(1, new Test()));
//save
#if 1
for (unsigned int i = 0; i< 100; ++i)
{
t[1]->c++;
save(t);
std::cout << t[1]->c << std::endl;
}
#else
//load
//This fails, c will always be the first serialized value
for (unsigned int i = 0; i< 100; ++i)
{
load(t);
std::cout << t[1]->c << std::endl;
}
#endif
}
</snip>