The application I am working on currently has a large number structs which contain data which is input from various sources such as data bases and files. For example like this:
struct A
{
float val1;
std::string val2;
int val3;
bool operator < (const A& other) const;
};
For processing, these structs are stored up in STL-containers, such as maps and therefore need a comparison operator. These are all the same and using simple boolean logic they can be written like this:
bool A:operator < (const A& o) const {
return val1 < o.val1 ||
(val1 == o.val1 && ( val2 < o.val2 ||
(val2 == o.val2 && ( val3 < o.val3 ) ) );
}
This seems efficient, but has several drawbacks:
- These expressions get huge if the structs as a dozen or more members.
- It is cumbersome to write and maintain if members change.
- It needs to be done for every struct separately.
Is there a more maintainable way to compare structs like this?