Sunday, 8 September 2013

Possible to automatically generate operator+ for a class whose data members all implement operator+?

Possible to automatically generate operator+ for a class whose data
members all implement operator+?

Given a plain-old-data C++ class or struct composed of types that
implement operator+:
struct VertexData
{
Vec4 vertex;
Vec2 texCoord;
};
Is it possible to use templates or some other trick to get the C++
compiler to automatically generate operator+ that adds each member, like
this?
VertexData operator+(VertexData const &a, VertexData const &b)
{
VertexData sum;
sum.vertex = a.vertex + b.vertex;
sum.texCoord = a.texCoord + b.texCoord;
return sum;
}

No comments:

Post a Comment