I believe the issues are:
- Need to return the
example
in make_example()
. You are probably getting a compiler warning here that you ignored.
- Need to
#include <boost/archive/binary_oarchive.hpp>
. Otherwise, it should not even compile.
Also, your comment // shouldn't use << as oa writes a text archive
is not quite correct because <<
is now overloaded for boost::archive::binary_oarchive
so that it is streaming binary.
Therefore, the modified code should look like:
#include <vector>
#include <string>
#include <bitset>
#include <fstream>
#include <boost/serialization/string.hpp>
#include <boost/serialization/vector.hpp>
#include <boost/serialization/bitset.hpp>
// ADD THIS!!!
#include <boost/archive/binary_oarchive.hpp>
template<size_t N>
struct Example
{
std::string id;
std::vector<std::bitset<N>> bits;
};
template<size_t N>
Example<N> make_example()
{
Example<N> example;
example.id = "some id";
example.bits.resize(100);
// ADD THIS!!!
return(example);
}
namespace boost
{
namespace serialization
{
template<typename Archive, size_t N>
void serialize ( Archive & a
, Example<N> & e
, const unsigned int version )
{
a & e.id;
a & e.bits;
}
}
}
int main()
{
auto example = make_example<256>();
std::ofstream ofs("filename", std::ios::binary);
boost::archive::binary_oarchive oa(ofs);
oa << example;
return(0);
}
A related example on SO is here.
Update To make the binary serialization of std::bitset
more compact
Look at the SO answer by @6502 here. You will then need to:
- Split your
serialize
function into separate load
and save
functions. See this (under Tutorial::Splitting serialize into save/load) for an example.
- In
save
, iterate over e.bits
and use @6502's bitset_to_bytes
function to convert EACH of the e.bits[i]
to a std::vector<unsigned char>
. You will then have a std::vector<std::vector<unsigned char>>
(a local variable in the save
function). Serialize that.
- Conversely, in
load
, unserialize to get a std::vector<std::vector<unsigned char>>
(again, local variable in load
). Then, iterate over that collection and use @6502's bitset_from_bytes<N>
function to convert EACH std::vector<unsigned char>
to e.bits[i]
.
- Remove the
#include <boost/serialization/bitset.hpp>
, you no longer need it.
This should bring the storage of each std::bitset<N>
from N
to (N+7)/8
bytes in the binary archive.
Hope this helps.