As an experiment, I am trying to (de)serialize an std::map<std:string, ValueType>
to/from a binary file, element by element.
I'm using something like this:
void Save(const std::string& fname, const MapType& c)
{
std::ofstream f(fname.c_str(), std::ios::binary);
boost::archive::binary_oarchive oa(f, boost::archive::no_header);
oa & c.size();
for (auto& e : c)
{
oa & e.first;
oa & e.second;
}
}
void Load(const std::string& fname, MapType& c)
{
std::ifstream f(fname.c_str(), std::ios::binary);
boost::archive::binary_iarchive ia(f, boost::archive::no_header);
int count;
ia & count;
for (int i = 0; i < count; ++i)
{
std::string key;
ValueType value;
ia & key;
ia & value;
c[key] = value;
}
}
where, incidentally:
using ValueType = boost::variant<bool, int, double, std::string>;
using MapType = std::map<std::string, ValueType>;
After saving, I load the file back. The count
is read correctly but the first key
gives a bad allocation exception.
Any ideas?
The same code works well when I replace binary_oarchive
and binary_iarchive
by their respective text equivalents.