P
PengYu.UT
Hi,
I have two questions.
1. Are there any good way to generalize the code for switch statement
with any number of cases?
2. Suppose test() is a very big function, and there are many cases in
the switch statement. The binary code could be huge. Are there any
method to deal with this problem.
Note: I have to path N as a template argument because the compiler can
optimize test much better than if I pass N as a function argument.
Thanks,
Peng
#include <iostream>
#include <cstdlib>
template <int N>
void test() {
std::cout << N << std::endl;
//Can be a very long function.
}
int main(int argc, char *argv[]) {
int n = atoi(argv[1]);
switch(n) {
case 0: test<0>(); break;
case 1: test<1>(); break;
case 2: test<2>(); break;
case 3: test<3>(); break;
case 4: test<4>(); break;
case 5: test<5>(); break;// Can be much more than 5 cases
default:
std::cout << "not found" << std::endl;
}
}
I have two questions.
1. Are there any good way to generalize the code for switch statement
with any number of cases?
2. Suppose test() is a very big function, and there are many cases in
the switch statement. The binary code could be huge. Are there any
method to deal with this problem.
Note: I have to path N as a template argument because the compiler can
optimize test much better than if I pass N as a function argument.
Thanks,
Peng
#include <iostream>
#include <cstdlib>
template <int N>
void test() {
std::cout << N << std::endl;
//Can be a very long function.
}
int main(int argc, char *argv[]) {
int n = atoi(argv[1]);
switch(n) {
case 0: test<0>(); break;
case 1: test<1>(); break;
case 2: test<2>(); break;
case 3: test<3>(); break;
case 4: test<4>(); break;
case 5: test<5>(); break;// Can be much more than 5 cases
default:
std::cout << "not found" << std::endl;
}
}