Passing Template Functions as parameters

Write here if you have problems with your C++ source code

Moderator: Board moderators

Post Reply
popel
New poster
Posts: 33
Joined: Fri Mar 15, 2002 2:00 am
Location: Dhaka, Bangladesh
Contact:

Passing Template Functions as parameters

Post by popel »

check the following code:
[cpp]
#include<iostream>
using namespace std;

template<class T>
void process(T a, T b,void (*job)(T a,T b))
{
cout<<"process called with "<<a<<" and "<<b;
cout<<endl;
job(a,b);
}

template<class T>
void print_max(T a,T b)
{
cout<<"max is ";
if(a>b)cout<<a;
else cout<<b;
cout<<endl;
}

template<class T>
void print_min(T a,T b)
{
cout<<"min is ";
if(a<b)cout<<a;
else cout<<b;
cout<<endl;
}
void main()
{
void (*p)(int a ,int b);
p = print_max;
p = print_min;
/* without these first three lines VC++ can't generate machine
code, but I think they are unnecessary */

process(10,20,print_max<int>);
process(20,50,print_min<int>);

}
[/cpp]

in brief, what I am doing is :
two similar template functions are there print_max(a,b) and print_min(a,b) which will print the max or min of two values.
There is a function process(a,b,void (*job)(a,b)) which work with the values a,b and do some job by the function passed.

[cpp]
process(10,20,print_max<int>);
process(20,50,print_min<int>);
[/cpp]

in these two lines I am saying the compiler explicitly to use the int version of print_max or print_min....
Is there any way to pass the template instead of original function ?
I mean it may look like :

[cpp]
template<class T>
void process(T a, T b,void (*job<T>)(T a,T b) )
/* this will not work */
[/cpp]
and I will call the function this way :
[cpp]
process(20,50,print_min);
[/cpp]
and should it work? :roll:
μδ. ταηνιπ αλ αμιη

Heartattack!
New poster
Posts: 45
Joined: Fri Jan 16, 2004 7:02 pm
Location: CSE::BUET
Contact:

Post by Heartattack! »

Oi Pops,
I believe what you want is not possible. I'm not sure, but this is my explanation:
Templates are just a blueprint. They do not contribute to the machine code. When the compiler encounters a call to a template, it "creates" a new function(if the same function with same data type/s hasn't been created before). This "creation" contributes to machine code. This "created" function has an address. The template itself has no address. Using templates does not change the machine code from that one created by using a seperate function for each data type/s. The template is used by the compiler. It's something like this:

Code: Select all

1.class POINT
{
public:
   int x,y;
};
2.POINT p;
Part 1 does not result in memory being used. You use memory by part 2:
POINT p;
You can then address this memory. Can you pass the address of POINT? [This is not exactly the same as templates, but it's easier to understand this way :wink: ]


The three lines you think are useless, are actually crucial. Because of the lines, two functions are created during compile time whos addresses you are passing with the call to process. Without the previous two lines, there would be no functions using the int version of print_max && print_min. This means no addresses of the functions. That's why the compiler won't compile the code-you'd be passing addresses that do not exist.

Hope this helps. See you on the 11th! 8) 8) 8) 8)
We will, We will BREAK LOOP!!!!

Post Reply

Return to “C++”