M
mathieu
Hi there,
I guess this is a pretty vague question but I stumble on it a couple
of times and never really knew what was a nice solution to it. I am
trying to avoid passing twice a template parameter when it could be
either deduced or simple reused. For instance I have a calculator
class that take a simple helper function that perform an operation on
the same type as my calculator,.
I would write something like this:
template <typename T>
struct Helper1
{
inline double operator() (T t) { return t; }
};
template <typename T>
struct Helper2
{
inline double operator() (T t) { return -t; }
};
template <typename T, typename THelper>
struct Calculator
{
static double compute(double t) {
THelper th;
return th(t);
}
};
int main()
{
Calculator<double, Helper1<double> > c;
return 0;
}
Notice how I write twice 'double' in
Calculator<double, Helper1<double> > c;
Thanks for suggestion,
-Mathieu
I guess this is a pretty vague question but I stumble on it a couple
of times and never really knew what was a nice solution to it. I am
trying to avoid passing twice a template parameter when it could be
either deduced or simple reused. For instance I have a calculator
class that take a simple helper function that perform an operation on
the same type as my calculator,.
I would write something like this:
template <typename T>
struct Helper1
{
inline double operator() (T t) { return t; }
};
template <typename T>
struct Helper2
{
inline double operator() (T t) { return -t; }
};
template <typename T, typename THelper>
struct Calculator
{
static double compute(double t) {
THelper th;
return th(t);
}
};
int main()
{
Calculator<double, Helper1<double> > c;
return 0;
}
Notice how I write twice 'double' in
Calculator<double, Helper1<double> > c;
Thanks for suggestion,
-Mathieu