Is it possible to avoid defining the same functions for structs A, B below twice? Their members are named exactly the same, but the v0, v1 ... vN members are of different type between the two structs A and B. If it helps the members v* are all derived from the same struct V.
Non-trivial functions (i.e. assignment =) can reuse a templated copy function outside the structs, like below, but it'd be preferred/cleaner if this was defined once and within the struct.
Is there a clean way to templatize A and B definition into a single one?
template <class T1, class T2>
void copy(T1& to, T2& from)
{
to.v0 = from.v0;
to.v1 = from.v1;
to.type = from.type;
}
enum class E { TYPE_0, TYPE_1 };
struct B;
struct A
{
C0<float> v0;
C1<int> v1;
E type;
A& operator = (const B& t)
{
copy(*this, t);
return *this;
}
string strType() { return string(type); }
};
struct B
{
D0<float> v0;
D1<int> v1;
E type;
B& operator = (const A& t)
{
copy(*this, t);
return *this;
}
string strType() { return string(type); }
}