I'm working on providing a set of serializing methods that need to be structure aware. I therefore built a class template that provides those methods to all class inheriting from it.
Here is what I have right now:
template<typename T>
class serializable
{
protected:
serializable() = default;
public:
size_t size_of() { return sizeof(T); }
void dump(void* data) { std::memcpy(data, this, serializable<T>::size_of()); }
void load(void* data) { std::memcpy(this, data, serializable<T>::size_of()); }
virtual void serialize()
{
std::cout << "Default serialize : size " << serializable<T>::size_of() << std::endl;
}
};
struct point : public serializable<point>
{
float p[3];
};
struct particle : public point, public serializable<particle>
{
float v[3];
};
int main()
{
particle p;
p.serialize();
return 0;
}
The issue is that when calling p.serialize
, the compiler issues an ambiguous overload error between serializable<point>::serialize()
and serializable<particle>::serialize()
.
How can I solve this? Is there any way of overriding the inheritance from serializable<point>
to only consider serializable<particle>
? Should I consider another approach?