Is it possible to write a template
Foo<int n>
such that:
Foo<2>
gives
switch(x) {
case 1: return 1; break;
case 2: return 4; break;
}
while
Foo<3>
gives
switch(x) {
case 1: return 1; break;
case 2: return 4; break;
case 3: return 9; break;
}
?
Thanks!
EDIT:
changed code above to return square, as many have guessed (and I poorly asked)
Yes, make a template with an oversized master switch
and hope/help the optimizer turns it into a little switch
. See my answer to your other question Runtime typeswitch for typelists as a switch instead of a nested if's?. Also, don't duplicate-post.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With