A lot of non-type template parameters

Viewed 99

I have a few functions that are very performance critical. They are quite generic and they depend on about 12 parameters apart from 2 inputs.

These parameters are fixed and I have 4 or 5 set of values (a1..a10) (b1..b10), etc... I could write the function a few times but for efficiency and maintainability I want to use non-type templates.

Imagine something like:

template <int a, int b, int c, int d, ..., int m>
double f(double x, double y) 
{ 
    return a*x+a*b*y+c+d+..+a*x*y; // some very complex math code
}

and it is only used in these N ways:

f<1,2,3,...,6>(x,y)
f<4,5,6,...,60>(x,y)
f<10,20,....,50,60>(x,y)

(in another application of the library, the set of parameters might be different but still only a few)

This is all fine but not very elegant...

I am looking for some "nicer" way to group these parameters in a cleaner way.

Ideas: - create many PARAMS types full of constexpr[s] - An abstract class with methods to be overriden (not sure if I can mix that with constexpr..)

I was wondering if there is some other nicer way or something available in boost that would be a good match for my problem.

EDIT: Something similar to this would be perfect! (clearly this is NOT working). And the most important thing is that I need compile time evaluation.

#include <iostream>

struct Params1
{
    constexpr static int a = 2;
    constexpr static double b = 4;
    constexpr static int c = 6;
};

struct Params2
{
    constexpr static int a = 1;
    constexpr static double b = 4.3;
    constexpr static int c = 3;
};

template<P>
double f(double x)
{
    return x*P.a*P.b*P.c;
};


int main() {

    std::cout << f<Params1>(1.2) << std::endl;
    std::cout << f<Params2>(1.2) << std::endl;

    return 0;
}
3 Answers
Related