Similarly to Preferred implementation of '<' for multi-variable structures I'm implementing a less-than operator for a structure with multiple values. I'm not worrying about using equality or less than operators, lets assume all the members correctly implement both. My structure has four fields and the operator is already getting quite messy:
struct Key {
std::string s_name;
bool b_mipmaps;
bool b_clamp_to_edge;
GLenum n_intformat;
// [constructors here]
bool operator <(const Key &other) const
{
return s_name < other.s_name || (s_name == other.s_name && (
b_mipmaps < other.b_mipmaps || (b_mipmaps == other.b_mipmaps && (
b_clamp_to_edge < other.b_clamp_to_edge || (b_clamp_to_edge == other.b_clamp_to_edge && (
n_intformat < other.n_intformat))))));
// compare two keys
}
}
I was wondering if there is a commonly used style of indentation or something that helps you not to get lost in the parentheses because frankly, it is hell and I imagine a bug in such an operator would be quite subtle and hard to track down / debug. Is there perhaps a way to break this down to some primitive functions? Or is there an STL function that does this?
I'm currently using C++03 but I'm open-minded about newer standards.