C++偏特化

#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;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值