将函数放到struct里,函数参数放在模板里,实现不同参数值执行不同的程序代码。
#include<iostream>
template <int MIN,int MAX>
struct mys {
static void print()
{
cout << "normal" << endl;//当(参数1!=参数2)时,执行此处;
}
};
template <int MIN>
struct mys<MIN,MIN> {
static void print()
{
cout << "unnormal" << endl;//当(参数1==参数2时,执行此处);
}
};
void main()
{
mys<3,3> a;
a.print();
mys<3,4>b;
b.print();
while (1);
}