architecture - C++ Compile code for all variants of finite number of settings -


for example, there hard logic inside function void func(long param1, long param2, long param3, long param4, long param5).

it has lot of statements inside depends on parameters, different checks, calculations depends on combinations , etc.

and function called many million times , takes serious amount of execution time. , i'd reduce time.

all parameters taken config.ini file, so, unknown @ compile time.

but know, param1 may in diapason [1..3], param2 in diapason [0..1] , etc.

so, finally, there maybe 200 combinations of parameters.

and i'd compiler compile separated 200 combination, , @ beginning of run-time, when config.ini loaded, choose 1 of them, , avoid run-time calculation of parameter's dependencies.

is possible achieve in c++98? or in c++11/14?

it possible using templates, since templates can have integers instead of type parameters. instance, following function

template <int iparam1, int iparam2> int sum() {     return iparam1 + iparam2; } 

is function in iparam1 , iparam2 fixed values specific template instantiation. example, function sum<1, 2> function returns 3.

in case, define func prototype:

template <long param1, long param2, long param3, long param4, long param5> void func() 

then, create std::map maps combination of parameters function in these parameters fixed. this:

using paramcombination = std::tuple<long, long, long, long, long>;  using paramstofunction = std::pair < paramcombination, std::function<void()> >;  std::map< paramcombination, std::function<void()> > map = {     paramstofunction(std::make_tuple<long, long, long, long, long>(1, 2, 2, 2, 2), func<1, 2, 2, 2, 2>),     ...     // map contain pairs of possible parameter combinations     // , corresponding functions. long list can     // generated using script.     }; 

these function have benefits of compile time optimizations. finally, during runtime, need create tuple represents parameter combination , call function tuple maps to:

auto comb = paramcombination(1, 2, 2, 2, 2); map[comb](); // function call 

Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

reactjs - React router and this.props.children - how to pass state to this.props.children -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -