#include <iostream>
using namespace std;
template<class TT = void,class P = void,class P2 = void>
class Function
{
public:
typedef TT (*PFUN)(P param,P2 param2);
Function(PFUN fun):m_fun(fun),m_p(0),m_p2(0){}
Function(PFUN fun,P p1,P2 p2):m_fun(fun),m_p(p1),m_p2(p2){}
TT operator()(P param,P2 param2)
{
return (*m_fun)(param,param2);
}
TT operator()()
{
return (*m_fun)(m_p,m_p2);
}
private:
PFUN m_fun;
P m_p;
P2 m_p2;
};
template<class TT,class P>
class Function<TT,P,void>
{
public:
typedef TT (*PFUN)(P param);
Function(PFUN fun,P p):m_fun(fun),m_p(p){}
Function(PFUN fun):m_fun(fun),m_p(0){}
TT operator()()
{
return (*m_fun)(m_p);
}
TT operator()(P param)
{
return (*m_fun)(param);
}
private:
PFUN m_fun;
P m_p;
};
template<class TT>
class Function<TT,void,void>
{
public:
typedef TT (*PFUN)();
Function(PFUN fun):m_fun(fun){}
TT operator()()
{
return (*m_fun)();
}
private:
PFUN m_fun;
};
template<>
class Function<void,void,void>
{
public:
typedef void (*PFUN)();
Function(PFUN fun):m_fun(fun){}
void operator()()
{
return (*m_fun)();
}
private:
PFUN m_fun;
};
//template<class TT,class P,class P2>
//void Print( Function<TT,P,P2> fn)
//{
// fn();
//}
void Print1(void)
{
cout<<"Print1"<<endl;
}
void Print2(int m)
{
cout<<"Print2:"<<m<<endl;
}
void Print3(double m)
{
cout<<"Print3:"<<m<<endl;
}
void Print4(double m,int m2)
{
cout<<"Print4:"<<m<<","<<m2<<endl;
}
int main()
{
Function<> fun1(Print1);
fun1();
Function<void,int> fun2(Print2);
fun2(2);
Function<void,double> fun3(Print3);
fun3(2.5456);
Function<void,double,int> fun4(Print4);
fun4(1.255,1);
getchar();
return 0;
}
using namespace std;
template<class TT = void,class P = void,class P2 = void>
class Function
{
public:
typedef TT (*PFUN)(P param,P2 param2);
Function(PFUN fun):m_fun(fun),m_p(0),m_p2(0){}
Function(PFUN fun,P p1,P2 p2):m_fun(fun),m_p(p1),m_p2(p2){}
TT operator()(P param,P2 param2)
{
return (*m_fun)(param,param2);
}
TT operator()()
{
return (*m_fun)(m_p,m_p2);
}
private:
PFUN m_fun;
P m_p;
P2 m_p2;
};
template<class TT,class P>
class Function<TT,P,void>
{
public:
typedef TT (*PFUN)(P param);
Function(PFUN fun,P p):m_fun(fun),m_p(p){}
Function(PFUN fun):m_fun(fun),m_p(0){}
TT operator()()
{
return (*m_fun)(m_p);
}
TT operator()(P param)
{
return (*m_fun)(param);
}
private:
PFUN m_fun;
P m_p;
};
template<class TT>
class Function<TT,void,void>
{
public:
typedef TT (*PFUN)();
Function(PFUN fun):m_fun(fun){}
TT operator()()
{
return (*m_fun)();
}
private:
PFUN m_fun;
};
template<>
class Function<void,void,void>
{
public:
typedef void (*PFUN)();
Function(PFUN fun):m_fun(fun){}
void operator()()
{
return (*m_fun)();
}
private:
PFUN m_fun;
};
//template<class TT,class P,class P2>
//void Print( Function<TT,P,P2> fn)
//{
// fn();
//}
void Print1(void)
{
cout<<"Print1"<<endl;
}
void Print2(int m)
{
cout<<"Print2:"<<m<<endl;
}
void Print3(double m)
{
cout<<"Print3:"<<m<<endl;
}
void Print4(double m,int m2)
{
cout<<"Print4:"<<m<<","<<m2<<endl;
}
int main()
{
Function<> fun1(Print1);
fun1();
Function<void,int> fun2(Print2);
fun2(2);
Function<void,double> fun3(Print3);
fun3(2.5456);
Function<void,double,int> fun4(Print4);
fun4(1.255,1);
getchar();
return 0;
}