M
Marcel Sebbao
I have a function that returns {sin(x),x,cos(x)} depending on a
parameter k that can take only 3 integer values: -1, 0, 1.
I am trying to do this with template specialization:
enum curv { negative=-1, zero=0, positive=1 };
template<curv k> double scurv(const double& x);
template<> double scurv<negative>(const double& x) { return sin(x); }
template<> double scurv<zero> (const double& x) { return x;}
template<> double scurv<positive> (const double& x) { return cos(x); }
curv guess_curv(const double& ym)
{
if (ym<0) return negative;
if (ym>0) return positive;
return zero;
}
Next, I would like to use this function in a class:
class Yfunc {
const curv k;
const double& y;
public:
inpa(const double& M, const double& Y)
: k(guess_curv(M+Y)), y(Y) {}
double ez(const double& a) const {
return scurv<k>(x) * pow(y*a, 0.6) ;
}
};
Such that I construct Yfunc once, k is determined, and I do not test
for k each time I call Yfunc::ez.
Template parameters do note allow this dynamical parameter setting .
Is there a smart way to overcome this, yet as performant as template
specialization? I was thinking of function pointer but it was not as
elegant.
Thanks.
parameter k that can take only 3 integer values: -1, 0, 1.
I am trying to do this with template specialization:
enum curv { negative=-1, zero=0, positive=1 };
template<curv k> double scurv(const double& x);
template<> double scurv<negative>(const double& x) { return sin(x); }
template<> double scurv<zero> (const double& x) { return x;}
template<> double scurv<positive> (const double& x) { return cos(x); }
curv guess_curv(const double& ym)
{
if (ym<0) return negative;
if (ym>0) return positive;
return zero;
}
Next, I would like to use this function in a class:
class Yfunc {
const curv k;
const double& y;
public:
inpa(const double& M, const double& Y)
: k(guess_curv(M+Y)), y(Y) {}
double ez(const double& a) const {
return scurv<k>(x) * pow(y*a, 0.6) ;
}
};
Such that I construct Yfunc once, k is determined, and I do not test
for k each time I call Yfunc::ez.
Template parameters do note allow this dynamical parameter setting .
Is there a smart way to overcome this, yet as performant as template
specialization? I was thinking of function pointer but it was not as
elegant.
Thanks.