https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87925
--- Comment #4 from Martin Liška <marxin at gcc dot gnu.org> --- (In reply to Eyal Rozenberg from comment #0) > Have a look at this GodBolt example: https://gcc.godbolt.org/z/zR03rA > > On one hand, we have: > > void foo(int i) { > switch (i) { > case 1: boo<1>(); break; > case 2: boo<2>(); break; > case 3: boo<3>(); break; > case 4: boo<4>(); break; > // etc. etc. > } > } > > on the other hand we have the same, but using an if-then-else chain: > > void bar(int i) { > if (i == 1) boo<1>(); > else if (i == 2) boo<2>(); > else if (i == 3) boo<3>(); > else if (i == 4) boo<4>(); > // etc. etc. > } > > The switch statement gets a jump table; the if-then-else chain - does not. > At the link, there are 20 cases; g++ starts using a jump table with 4 switch > values. Agree with that, but I would say that when having a lot of cases, switch is more natural constructor. The code looks nicer ;) > > This is not just a matter of programmers needing to remember to prefer > switch statements (which it's better not to require of them), but rather > that if-then-else chains are sometimes generated by expansion of templated > code, e.g. this example for checking for membership in a set of values (= > all values of an enum): > > https://stackoverflow.com/a/53191264/1593077 This is not problem because it's a const expression that is evaluate in C++ front-end. Thus you don't get any penalty.