motivation: I would like to create a utility class so that instead of having to write:
if( someVal == val1 || someVal == val2 || someVal == val3 )
I could instead write:
if( is(someVal).in(val1, val2, val3) )
which is much closer to the mathematical 'a is an element of (b,c,d)' and also would save on a lot of typing when the variable name 'someVal' is long.
Here is the code I have so far (for 2 and 3 values):
template<class T>
class is {
private:
T t_;
public:
is(T t) : t_(t) { }
bool in(const T& v1, const T& v2) {
return t_ == v1 || t_ == v2;
}
bool in(const T& v1, const T& v2, const T& v3) {
return t_ == v1 || t_ == v2 || t_ == v3;
}
};
However it fails to compile if I write:
is(1).in(3,4,5);
instead I have to write
is<int>(1).in(3,4,5);
Which isn't too bad, but it would be better if somehow the compiler could figure out that the type is int
with out me having to explicitly specify it.
Is there anyway to do this or I am stuck with specifying it explicitly?