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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
在C++中,模板是一种通用的编程工具,可以在编译时生成不同类型的代码。是模板的两种殊形式,用于定制模板在定类型或条件下的行为。 是指为模板提供一个定类型的实现,当模板被实例时,编译器会优先选择版本。版本可以完全替代通用版本,通常用于优性能或处理殊情况。 是指为模板提供一个部分的实现,当模板被实例时,编译器会根据匹配度选择版本或通用版本。版本不能完全替代通用版本,通常用于处理复杂类型或多个参数的情况。 以下是的示例: 示例: ```cpp template <typename T> class MyTemplate { public: void myFunc() { std::cout << "This is the generic version." << std::endl; } }; template<> class MyTemplate<int> { public: void myFunc() { std::cout << "This is the specialized version for int." << std::endl; } }; int main() { MyTemplate<double> myDoubleObj; myDoubleObj.myFunc(); // This is the generic version. MyTemplate<int> myIntObj; myIntObj.myFunc(); // This is the specialized version for int. return 0; } ``` 在上面的示例中,MyTemplate类有一个通用版本和一个版本,当模板实例为int类型时,编译器会选择版本,输出定的信息。 示例: ```cpp template <typename T, typename U> class MyTemplate { public: void myFunc() { std::cout << "This is the generic version." << std::endl; } }; template <typename T> class MyTemplate<T, int> { public: void myFunc() { std::cout << "This is the partial specialization for T and int." << std::endl; } }; int main() { MyTemplate<int, double> myObj1; myObj1.myFunc(); // This is the generic version. MyTemplate<double, int> myObj2; myObj2.myFunc(); // This is the partial specialization for T and int. return 0; } ``` 在上面的示例中,MyTemplate类有一个通用版本和一个版本,当模板实例为T和int类型时,编译器会选择版本,输出定的信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值