@cde, 'C' programming doesn't have a construct to use the expression like in the first form, so you have to use the second form, as many have already mentioned above.
You might try the following approach, if the values are known ahead of comparison, either by including them at the time of declaration or populate with the values at a later time (which might of fixed or variable set, in which case the array has to be allocated dynamically and size must have already been available by then).
Note: inline, static, const aren't really necessary for the solution to work. Also, it has the advantage that the function returns as soon as the condition evaluates to true skips the rest of the comparisons, unless the state doesn't match with any of the given states.
#include <iostream>
using namespace std;
inline bool isInKnownStates(int state, const int ds[], const int sz)
{
int i = 0;
for (; (i < sz) && (state != ds[i]); i++);
return (i < sz);
}
int main(int argc, const char * argv[]) {
static const int discrete_states[] = {1, 3};
static const int sz = sizeof(discrete_states)/sizeof(discrete_states[0]);
int state0 = 0;
int state1 = 1;
int state3 = 3;
cout << state0 << " " << (isInKnownStates(state0, discrete_states, sz) ? "True" : "False") << endl << flush;
cout << state1 << " " << (isInKnownStates(state1, discrete_states, sz) ? "True" : "False") << endl << flush;
cout << state3 << " " << (isInKnownStates(state3, discrete_states, sz) ? "True" : "False") << endl << flush;
return 0;
}
6.5 Expressions?#define TEST2(var, a, b) ( (var) == (a) || (var) == (b) ). (bear in mind that this evaluatesvartwice still). You could write an inline function to avoid the double evaluation. If you have many cases then use aswitch.TEST2(a++, 1, 3)and the behavior is well defined. That's a wonderful thing, and kind of rare for macros.statehave? 0, -1, 1000000, 1.23?