Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ function vs template with an integer parameter?

Tags:

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?