0

I am trying to serialize the following user-defined object:

ConcatConstMapping<std::multiplies<double> >* obj;

Boost shows me the following error:

> /usr/include/boost/serialization/access.hpp:118:9: error: ‘struct
> std::multiplies<double>’ has no member named ‘serialize’

This is how class ConcatConstMapping looks like:

template<class Operator>
class ConcatConstMapping: public ConstMapping
{
protected:

    typedef std::pair<Dimension, Argument::const_iterator> DimIteratorPair;
    typedef std::list<ConstMapping*> MappingSet;
    MappingSet            mappings;
    ConstMapping*         refMapping;

    bool                  continueOutOfRange;
    Argument::mapped_type oorValue;

    Operator op;

    friend class boost::serialization::access;

    template<class Archive>
    void serialize(Archive & archive, const unsigned int version)
    {
        archive & mappings;
        archive & refMapping;
        archive & continueOutOfRange;
        archive & oorValue;
        archive & op;
    }
};

Edit: The error is gone when I comment line archive & op;. But I fill I need to add that line too for serialization.

ManiAm
  • 1,759
  • 5
  • 24
  • 43

2 Answers2

0

For custom classes, you will need to declare the serialize function yourself, either as part of the class or as a free function, as explained in the documentation.

template<class Archive>
inline void serialize(
    Archive & ar, 
    my_class & t, 
    const unsigned int file_version
) {
    // For example:
    ar & t.member_one;
    ar & t.member_two;
    ....
}
Philipp Ludwig
  • 3,758
  • 3
  • 30
  • 48
  • I already know that! how can I do this for std::multiplies struct ? Your answer seems to be general. – ManiAm Apr 20 '17 at 19:29
  • 1
    @ManiAm Key phrase is "free function". That `my_class` from the example quoted by Philipp should be replaced by std::multiplies. In fact, the passage in the documentation that the quoted example is in calls out specifically the STL as a use-case for the free function approach, since you can't add methods to them. – Altainia Apr 20 '17 at 19:39
  • Now after looking closer, I'm not sure if you actually can serialize this, since it seems that ``std::multiplies`` doesn't store any data. – Philipp Ludwig Apr 20 '17 at 19:40
  • @PhilippLudwig but why boost is throwing me an error ? – ManiAm Apr 20 '17 at 19:41
0

I am not sure why would you like to serialize multiplies since, it doesnt store any data... so there is nothing to serialize. To satisfy the compiler, this should do:

template<class Archive>
inline void serialize(
    Archive & ar, 
    std::multiplies<double> & t, 
    const unsigned int file_version
) { }

If you provide this, the compiler will pick up a matching function... which does nothing.

I guess you could futher template this for multiplies<T>.

multipilies does not have a state, it's merely a functor wrapper around lhs*rhs. I'd advise you to reconsider your design to avoid serialization at all, rather than working around the error by serializing nothing.

luk32
  • 15,812
  • 38
  • 62
  • thanks! I edited my code to show the class ConcatConstMapping content. It might help in figuring out why Boost is showing me error. – ManiAm Apr 20 '17 at 19:49