Reading the Wikibook Optimizing C++, in this paragraph there is the following suggestion:
If an integer value is a constant in the application code, but is a variable in library code, make it a template parameter.
So if I have a function like
void myfunction(int param)
{
switch(param)
{
case 1:
do_something_1();
break;
case 2:
do_something_2();
break;
...
case 100: // 100 is taken as example
do_something_100();
break;
}
}
Is convenient to replace it with the following?
template<int param> void myfunction()
{
switch(param)
{
case 1:
do_something_1();
break;
case 2:
do_something_2();
break;
...
case 100: // 100 is taken as example
do_something_100();
break;
}
}
Or is completely unnecessary? Could you please explain to me the reason?
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