473,509 Members | 7,333 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Repeating template parameter

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
Dec 10 '07 #1
4 1491
On 10 Pro, 15:32, mathieu <mathieu.malate...@gmail.comwrote:
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,
template<typename Tclass THelper
>
struct Calculator
Dec 10 '07 #2
On 10 Dec, 15:32, mathieu <mathieu.malate...@gmail.comwrote:
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<typenameclass THelper>
struct Calculator
{
static double compute(double t) {
THelper<Tth;
return th(t);
}

};

int main()
{
Calculator<double, Helper1c;
double t = c.compute(5);
return 0;
}
Hope this helps!

--
Daniel
Dec 10 '07 #3
On Dec 10, 7:32 pm, mathieu <mathieu.malate...@gmail.comwrote:
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<typenameclass THelper>
struct Calculator
{
static double compute(double t) {
THelper<Tth;
return th(t);
}
};
Dec 10 '07 #4
On Dec 10, 4:58 pm, Abhishek Padmanabh <abhishek.padman...@gmail.com>
wrote:
On Dec 10, 7:32 pm, mathieu <mathieu.malate...@gmail.comwrote:
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<typenameclass THelper>
struct Calculator
{
static double compute(double t) {
THelper<Tth;
return th(t);
}

};
Thanks ! I never used template template parameter before :)

pretty cool indeed !

-Mathieu
Dec 10 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
2117
by: Lionel B | last post by:
Greetings. The following code compiles ok and does what I'd expect it to do: ---------- START CODE ---------- // test.cpp
8
3135
by: Tony Johansson | last post by:
Hello Experts! What does this mean actually. If you have a template with a type and non-type template argument, say, like this template<typename T, int a> class Array {. . .}; then A<int,...
2
2743
by: Siegfried Weiss | last post by:
Hi guys, i give up finding a solution by reading or by trial & error. Hope, YOU can help me! (Sorry for my rather long posting.) Stroustrup says, that templates could be declared with - type...
3
1726
by: Erik Wikström | last post by:
I've been trying for a while now to understand how template template parameters work. But I just can't wrap my head around it and was hoping that someone might help me. As best I can figure the...
11
4129
by: Christoph Boget | last post by:
When building a form using Infopath, you can define a repeating section and stick form fields in that section. I'm curious if ASP.NET has a similar control to make it easy to design something...
1
2189
by: Kai-Uwe Bux | last post by:
Hi folks, I would like to know which clause of the standard rules out this: template < typename eval > struct recursive_template { typedef typename eval::enum_type Enum;
8
5302
by: Jess | last post by:
Hi, I have a template function that triggered some compiler error. The abridged version of the class and function is: #include<memory> using namespace std; template <class T>
4
3003
by: David Sanders | last post by:
Hi, I have a class with an integer template parameter, taking values 1, 2 or 3, and a function 'calc' in that class which performs calculations. Some calculations need only be performed if the...
2
2361
by: ndbecker2 | last post by:
On upgrading from gcc-4.1.2 to gcc-4.3, this (stripped down) code is now rejected: #include <vector> #include <iostream> template<typename T, template <typename Aclass CONT=std::vector>...
0
7137
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7347
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7073
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5656
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5062
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4732
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3207
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
779
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
443
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.