motivation: I would like to create a utility class so that instead of writing:
if( someVal == val1 || someVal == val2 || someVal == val3 )
Instead, I could write:
if( is(someVal).in(val1, val2, val3) )
which is much closer to math, a is an element of (b, c, d) ', and will also save a lot of messages when the variable name' someVal 'is long.
Here is the code that I still have (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 does not compile if I write:
is(1).in(3,4,5);
instead I have to write
is<int>(1).in(3,4,5);
This is not so bad, but it would be better if somehow the compiler could understand that the type is int , and I need to explicitly specify it.
Is there anyway to do this, or am I stuck specifying it explicitly?
source share