For a 'tuple-style' C++ struct, such as
struct state {
int a, b, c;
};
how can we most quickly operator<
and constructors with natural semantics (e.g. similar to those of std::pair
)?
Specifically, is there a way to obtain the following methods without explicitly writing the implementation?
struct state {
int a, b, c;
state() { } // a = b = c = 0
state(int _a, int _b, int _c) { } // a = _a, b = _b, c = _c
bool operator<(const state& other) const {
// first compare by a, then by b, then by c
return (a == other.a ? (b == other.b ? (c < other.c) : b < other.b) : a < other.a)
}
};
More generally, does any approach for when the fields are not all of the same type? For example,
struct state {
int a, b;
string c;
};