C ++ function vs template with integer parameter?

Reading Wikibook C ++ Optimization , this section suggests the following sentence:

If the integer value is a constant in the application code, but is a variable in the 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 it 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 absolutely not necessary? Could you explain the reason to me?

+7
source share
3 answers

The skill that you have in mind only works if the parameter is known at compile time. Your quote has an assumption about the application code, which you cannot do when writing the library.

If your function calls the application code used

 const int x = 3; myfunction(1); myfunction(2); myfunction(x); //etc... 

They can be rewritten as follows.

 const int x = 3; myfunction<1>(); myfunction<2>(); myfunction<x>(); //etc... 

But if x is a variable, this is not possible:

 int x = ...; // unknown at compile-time! myfunction<x>(); // will fail to compile! 

As stated above, there are times when you should not make assumptions about the application when writing the library. Sometimes you need or need to do. Consider the case where you expect the application to use a constant, but you do not want this to be done.

You want to optimize for the case when it will use a constant but still allows the use of a variable . strong>. For this, I offer two options:

  • Make two options: one with a template parameter and one with a function parameter.

  • Inline , therefore, when compiling the application code, the definition of the function is considered by the compiler and can be used to optimize it with a single call to do_something_*() , if the parameter was constant.

Please note that both options require mapping the function definition to the application code. I would prefer to use the second option.

+6
source

It depends on how you want to use myfunction . For example, your template function cannot be used with a variable declared at runtime:

 int dosomethingmaybe = 1; dosomethingmaybe += 2; myfunction< dosomethingmaybe >(); // <--- Error, you cannot instantiate a template with a non-constant variable myfunction( dosomethingmaybe ); // <--- Will call `do_something_3();`, according to your code 

You use templates when you can provide an argument at compile time that will not be changed at run time. Templates are always evaluated at compile time, and therefore their inputs and outputs are fixed before the program runs !

Create a version of the template if you know that someone can correct the input and expect a certain output of functions ahead of time. Otherwise, the regular version version is excellent.

+3
source

I doubt that you really will see a performance advantage in real situations. If the call is built-in, there is no difference between the two approaches - as long as the parameter is known at compile time (what it should be), a decent compiler will remove the unnecessary switch in both cases. The only time you see the difference is that the attachment does not occur - in this case, the template approach will remove the switch, and the other will not. However, the function call overhead is likely to underestimate the cost of the switch anyway.

+1
source

All Articles