I\'ve always been under the impression that for any comparison statement, i.e. X == Y
or X != Y
is the format, and you chain statements together with <
With operator overloading, you might be able to get the exact syntax that you want. But, as Adam points out, that could lead to excluding valid expressions.
Below is a template with operator overloading, a template function, and a macro to achieve a syntax similar to Mooing Duck's nicer solution, but without requiring C++11, and allowing the use of the ||
operator to denote the "haystack" collection.
template
struct MultiOrComparable {
mutable std::set vals;
const MultiOrComparable & operator || (T v) const {
vals.insert(v); return *this;
}
bool operator == (T v) const { return vals.find(v) != vals.end(); }
};
template
MultiOrComparable MultiOrComparableStart (T) {
return MultiOrComparable();
}
#define IsOneOf(x, y) ((MultiOrComparableStart(x)||y) == x)
Then, the following program "works":
enum Foo { A, B, C, D };
int
main ()
{
if (!IsOneOf(A, B || C || D)) {
std::cout << "!=" << std::endl;
}
if (IsOneOf('a', 'x' || 'y' || 'z' || 'a')) {
std::cout << "==" << std::endl;
}
}