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);
They can be rewritten as follows.
const int x = 3; myfunction<1>(); myfunction<2>(); myfunction<x>();
But if x is a variable, this is not possible:
int x = ...;
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.
leemes
source share