Is it possible to define some kind of template that can create a generic comparable operator for structs?
For example is it possible for something like this?
struct A
{
int one;
int two;
int three;
};
bool AreEqual()
{
A a {1,2,3};
A b {1,2,3};
return ComparableStruct<A>(a) == ComparableStruct<A>(b);
}
All this does is a field by field comparison of the structs. You can assume all fields are of basic types or have overloaded operator==.
I have a lot of structs like this and it would save me a lot of time if I can just put it in a template or something for comparison rather than defining an operator== for every single struct. Thanks!
Update
It seems like this is not possible with C++. I wonder why this is voted out of C++ proposals, if anyone has the reasons let us know!
For solution that works with basic types only see solution by R Sahu.