Boost logo

Boost Users :

Subject: Re: [Boost-users] [serialization] how to serialize boost::function
From: Robert Ramey (ramey_at_[hidden])
Date: 2009-09-15 12:36:24


An interesting topic which I've yet to find time to address.

You're trying to serialize a pointer to a function. This is not implemented
within the library.

I would suggest a couple of ideas.

Make each f1, f2... a function object.
Make a boost::variant<f1, f2, ...> and serialize that. The library already
supports serialization of variants.

Another idea would be to dervive all function objects from a common base

struct fbase {
    virtual void operator()(int a) = 0; // makes fbase polymorphic
}

and serialize through a base class pointer

struct foo {
    fbase * f;
    std::vector<fbase *> functors_;
    void init(){
        functors_.pushback(& f1...);
};

Robert Ramey

Igor R wrote:
> Hello,
>
> Is it possible to serialize the following class:
>
> struct foo
> {
> void f1(int a)
> {}
> void f2(const std::string& s)
> {}
>
> void init()
> {
> using boost::bind;
> functors_.push_back(bind(&foo::f1, this, 1));
> functors_.push_back(bind(&foo::f2, this, "test"));
> }
>
> std::vector<boost::function<void(void)> > functors_;
> };
>
> That is, I'd like save the functors, in order to run them later, when
> the object is loaded from the archive.
>
> A straightforward attempt fails:
>
> struct foo
> {
> //......
> friend class boost::serialization::access;
> template<class Archive> void serialize(Archive &ar, const unsigned
> int version) {
> ar & BOOST_SERIALIZATION_NVP(functors_);
> }
> };
>
> 1>d:\dev\servision\projects\windows\thirdparty\boost\boost\serialization\access.hpp(109)
>> error C2039: 'serialize' : is not a member of
> 'boost::function<Signature>'


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