I am using C++ for particle physics and I find myself commondly writing lines like this:
bool isItCorrect(int i){
if(i==11 || i == 62 || i==-11 || i == 11002 || i==22002) return True
else return false;
}
What is the easiest way for me to make this shorter in C++. In python I could do:
def isItCorrect( i ):
if (i is in [11,62,-11,11002,22002]) return True
else return False
You can use variadic templates in C++ 11 and define:
template <typename T, typename T1>
bool isItCorrect(T t, T1 t1) {
return t == t1;
}
template <typename T, typename T1, typename... T2>
bool isItCorrect(T t, T1 t1, T2... t2) {
return t == t1 || isItCorrect(t, t2...);
}
and use:
bool isItCorrect(int i) {
return isItCorrect(i, 62, -11, 110022, 22002);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With