views:

52

answers:

1

I have a "generator" class that basically constructs its subclass. To use this thing I simply subclass it and pass it the correct parameters to build the object I want built. I want to serialize these things and there's no good reason to do it for each subclass since all the data is in the base. Here's what I've got as example:

#include <boost/serialization/serialization.hpp>
template < typename T >
struct test_base
{
  // works...
  //template < typename Archive >
  //void serialize(Archive &, unsigned int const)
 // {
  //}
};

template < typename T >
void f(test_base<T> const&) {}

struct test_derived : test_base<int>
{
};

namespace boost { namespace serialization {

template < typename Archive, typename T >
void serialize(Archive &, test_base<T> &, unsigned int const)
{
}

}}

#include <boost/archive/binary_oarchive.hpp>
#include <sstream>
int main()
{
  int x = 5;
  test_derived d;
  //boost::serialization::serialize(x, d, 54); // <- works.

  std::ostringstream str;
  boost::archive::binary_oarchive out(str);
  out & d; // no worky.
}

I want the free version to work if possible. Is it?

Version above pukes up error about serialize not being a member of test_derived.

A: 

Clarification why the problem happens:
boost::serialization has to ways of implementing the serialize function. As class method or (in your case) the non-intrusive way of defining a function in the boost::serialization namespace.
So the compiler has to somehow decide which implementation to choose. For that reason boost has a 'default' implementation of the boost::serialization::serialize template function.
Signature:

template<class Archive, class T>
inline void serialize(Archive & ar, T & t, const BOOST_PFTO unsigned int file_version)


Within that function there is a call to T::serialize(...). So when you don't want the intusive version you have to override the boost::serialization::serialize function with something more explicit than the default function-template.
Now the problem:
In your case the compiler has to decide if it
a) chooses the version where a parameter has to be casted implicit (test_derived& to test_base&)
b) use the generic function without casting (T is test_derived&)
You want the compiler to use variant a) but the compiler prefers b)

Solution:
I don't know a really good solution. I think i would go with a macro which generates implementations of serialize(...) with the explicit type.
If that isn't a possible solution for you, you could also tell the compiler more explicit what to call:

out & *((test_base<int>*)&d);


and wrap it in some helper function (because no one wants to look at such code all the day)

I hope that is a clear description and helps

In case my explanation was not clear, here is an example:

#include <iostream>
class Base
{
public:
    virtual ~Base()
    {
    }
};

class Derived : public Base
{
public:
    virtual ~Derived()
    {
    }
};


void foo(Base& bar)
{
    std::cout << "special" << std::endl;
}

template<typename T>
void foo(T& bar)
{
    std::cout << "generic" << std::endl;
}

int main()
{
    Derived derived;
    foo(derived);         // => call to generic implementation
    foo(*((Base*) &bla)); // => call to special 
    return 0;
}
Daniel