|
Boost Users : |
From: Ken Roser (boost_at_[hidden])
Date: 2006-11-22 16:17:13
Following is a very short program to test serialization along with the
g++ output when compiling it. My problem arises when I try to write a
serialization function for a bitfield. As the compiler output shows,
the bitfield is the cause of this problem.
How should one serialize a bitfield? Have I made some mistake in this
code?
I can get seriailziation to work if I instead use a save() and load()
function that copies each bit to an int before archiving but I'm unsure
if this is the best way.
#include <stdlib.h>
#include <string>
#include <iostream>
#include <sstream>
#include <boost/archive/archive_exception.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/serialization/export.hpp>
#include <boost/serialization/serialization.hpp>
#include <boost/serialization/list.hpp>
class Event
{
friend class boost::serialization::access;
template<class Archive> void serialize(Archive &ar, const int version)
{
ar & bits;
ar & field1;
};
public:
struct s
{
friend class boost::serialization::access;
template<class Archive> void serialize(Archive &ar, const int
version)
{
ar & spare1;
};
int spare1 : 1;
} bits;
int field1;
};
main(int argc, char **argv)
{
Event *evt = new Event;
evt->field1 = 0x1234;
std::stringstream ofs;
boost::archive::binary_oarchive oa(ofs);
oa & *evt; // serialize the event
}
serial2.cpp: In member function 'void Event::s::serialize(Archive&, int)
[with Archive = boost::archive::binary_oarchive]':
boost/serialization/access.hpp:109: instantiated from 'static void
boost::serialization::access::serialize(Archive&, T&, unsigned int)
[with Archive = boost::archive::binary_oarchive, T = Event::s]'
boost/serialization/serialization.hpp:81: instantiated from 'void
boost::serialization::serialize(Archive&, T&, unsigned int) [with
Archive = boost::archive::binary_oarchive, T = Event::s]'
boost/serialization/serialization.hpp:140: instantiated from 'void
boost::serialization::serialize_adl(Archive&, T&, unsigned int) [with
Archive = boost::archive::binary_oarchive, T = Event::s]'
boost/archive/detail/oserializer.hpp:147: instantiated from 'void
boost::archive::detail::oserializer<Archive,
T>::save_object_data(boost::archive::detail::basic_oarchive&, const
void*) const [with Archive = boost::archive::binary_oarchive, T = Event::s]'
serial2.cpp:43: instantiated from here
serial2.cpp:29: error: cannot bind bitfield
'((Event::s*)this)->Event::s::spare1' to 'int&'
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