Repeating template parameter

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
 
O

Ondra Holub

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

Try this:

template<
typename T,
struct Calculator
 
V

Victor Bazarov

Ondra said:
Try this:

template<
typename T,

struct Calculator

In order to instantiate the local variable 'th' in the member 'compute'
of 'Calculator', the second argument (as currently written) has to be
a concrete class, it cannot be a template. With your suggestion, the
'Calculator::compute' would have to be rewritten as

static double compute(double t) {
THelper<T> th;
return th(t);
}

V
 
D

Daniel Lidstrom

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;

If you're using a modern C++ compiler (VC7.1 or newer, gcc 3.4, etc)
you can use template template parameters. Here's what it will look
like:

template <typename T, template<typename> class THelper>
struct Calculator
{
static double compute(double t) {
THelper<T> th;
return th(t);
}

};

int main()
{
Calculator<double, Helper1> c;
double t = c.compute(5);
return 0;
}


Hope this helps!
 
A

Abhishek Padmanabh

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;

You can make THelper as a template template parameter to the
calculator template as below:

template <typename T, template<typename> class THelper>
struct Calculator
{
static double compute(double t) {
THelper<T> th;
return th(t);
}
};
 
M

mathieu

You can make THelper as a template template parameter to the
calculator template as below:

template <typename T, template<typename> class THelper>
struct Calculator
{
static double compute(double t) {
THelper<T> th;
return th(t);
}

};

Thanks ! I never used template template parameter before :)

pretty cool indeed !

-Mathieu
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top