[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?
