Maximum number of cases in switch?

The question is simple but the answer may not be? Please reply only if you are very sure of the answeer

How many maximum number of cases can be there in a switch statement?
Why don't you try it out?
1
2
3
4
5
6
7
8
9
10
#include <fstream>
#include <cstdlib>

int main(){
    std::ofstream file("output.cpp");
    file <<"#include <cstdlib>\n\nint main(){\n\tint a=rand();\n\tswitch(a){\n\t";
    for (int a=0;a<1000000;a++)
        file <<"\tcase "<<a<<": return a+"<<rand()<<";\n\t";
    file <<"}\n}\n";
}

Note that this belongs to the class of questions that if you have to ask, you're probably already doing something wrong.
Last edited on
From a doc i have, it is said that
1
2
Standard C specifies that a switch can have at least 257 case statements. Standard
C++ recommends that at least 16,384 case statements be supported!

The real value must be implementation dependant.
But if you need 16384 cases, i'm not sure if a switch is still the best solution. Maybe a hash_map of function pointers?
Topic archived. No new replies allowed.