1. ## Switch statement

Is it possible to have a switch statement use more than one variable? i.e., something like:

Code:
```switch (n && nn) {

case 1 && 2:
cout<< "n was 1 and nn was 2." <<endl;
break;

case 2 && 1:
cout<< "n was 2 and nn was 1." <<endl;
break;

default:
cout<< "n wasn't 1 or 2 and neither was nn." <<endl;

}```

2. No, not in the sense you expect, but you can use nested switches. Or two followed by each other.

3. Originally Posted by Elysia
No, not in the sense you expect, but you can use nested switches. Or two followed by each other.
Ok. So something like this then:

Code:
```switch(n) {

case 1:
switch(nn) {
case 2:
cout<< "n is 1 and nn is 2." <<endl;
default:
cout<< "n is 1 and nn is not 2." <<endl;
}

case 2:
switch(nn) {
case 1:
cout<< "n is 2 and nn is 1." <<endl;
default:
cout<< "n is 2 and nn is not 1." <<endl;
}

default:
cout<< "n is neither 1 or 2." <<endl;
}```

4. That works if you must have it so. It all depends on what you want to do.
Sometimes you can simplify it with some logic twists.

5. I guess you could cram two small ints into a regular int and use macro magic to have this functionality:
Code:
```switch( SHORT_PAIR(n,nn) )
{
case SHORT_PAIR(1,1):
// . . .
break;
case SHORT_PAIR(1,2):
// . . . etc
};```
But that would be a hackish, inelegant solution.

6. Speaking of which, with constexpr, we should be able to do the same thing without macros in C++0x.
Ah, but we could also use some template trickery for the cases and a function for the switch header, too.
I suppose I could demonstrate if there are any interested parties.

7. I thought there was a template solution, but I'm not clear on how you make a template evaluate to only a constant expression. I'm interested in your demonstration.

8. But your case labels are constant expressions, no?
Assuming we're stuffing two shorts into an int:
Code:
```template<short N1, short N2> struct ShortPair { static const int N = (N1 << 16) + N2; };

int MakeShortPair(short N1, short N2)
{
return (N1 << 16) + N2;
}

int main()
{
switch (MakeShortPair(10, 20))
{
case ShortPair<10, 20>::N: /* Do something */ ; break;
}
}```

9. I guess Elysia meant something like this:

Code:
```#include <iostream>
using namespace std;

unsigned combine_small_values(int a, int b)
{
return a | (b << 16);
}

template <unsigned a, unsigned b>
struct combine_small_values_at_compile_time
{
static const unsigned value = a | (b << 16);
};

int main()
{
int n = 2, nn = 1;
switch (combine_small_values(n, nn)) {

case combine_small_values_at_compile_time<1, 2>::value:
cout<< "n was 1 and nn was 2." <<endl;
break;

case combine_small_values_at_compile_time<2, 1>::value:
cout<< "n was 2 and nn was 1." <<endl;
break;

default:
cout<< "n wasn't 1 or 2 and neither was nn." <<endl;

}
}```
Perhaps also try something along the lines of:
Code:
```    if ((n == 1 || n == 2) && (nn == 1 || nn == 2)) {
cout << "n was " << n << " and nn was " << nn << ".\n";
}
else {
cout << "n or nn was neither 1 nor 2.\n";
}```

10. Cool. I didn't think of a template struct with a static const member. That opens up my head a bit.