你可能会看到类似下面的模板定义:
#include <cstdlib>
#include <iostream>
using namespace std;
template<class T>
T Minus(T a, T b)
{
return a - b;
}
template<class T>
class Add
{
public:
T add(T a, T b)
{
return a + b;
}
};
int main(int argc, char *argv[])
{
cout<<Minus(3, 4)<<endl;
cout<<Minus<float>(0.3, 0.4)<<endl;
Add<double> ap;
cout<<ap.add(9, 8)<<endl;
cout<<ap.add(0.001, 0.1)<<endl;
cout << "Press the enter key to continue ...";
cin.get();
return EXIT_SUCCESS;
}
编译运行结果如下:
为什么class可以用来定义模板参数呢?为什么还要引进typename呢?
在类中可以定义其它的新类型
#include <cstdlib>
#include <iostream>
using namespace std;
class Test
{
public:
typedef int* PINT;
struct Point
{
int x;
int y;
};
class Sub
{
public:
Sub()
{
cout<<"Sub()"<<endl;
}
void print()
{
cout<<"Hello World"<<endl;
}
};
};
int main(int argc, char *argv[])
{
Test::PINT pi = new int(5);
Test::Point po = {2, 3};
Test::Sub sub;
cout<<*pi<<endl;
cout<<po.x<<" "<<po.y<<endl;
sub.print();
delete pi;
cout << "Press the enter key to continue ...";
cin.get();
return EXIT_SUCCESS;
}
编译运行结果如下:
在类模板中定义新的类型
在函数模板中使用类模板的内部类型
#include <cstdlib>
#include <iostream>
using namespace std;
template<typename T, int N>
class Test
{
public:
typedef T ElemType;
enum { LEN = N };
ElemType array[LEN];
};
template<typename T>
void test_copy(T& test, typename T::ElemType a[], int len)
{
int l = (len < T::LEN) ? len : T::LEN;
for(int i=0; i<l; i++)
{
test.array[i] = a[i];
}
}
int main(int argc, char *argv[])
{
Test<int, 5> t1;
Test<float, 3> t2;
int ai[] = {5, 4, 3, 2, 1, 0};
float af[] = {0.1, 0.2, 0.3};
test_copy(t1, ai, 6);
test_copy(t2, af, 3);
for(int i=0; i<5; i++)
{
cout<<t1.array[i]<<endl;
}
for(int i=0; i<Test<float, 3>::LEN; i++)
{
cout<<t2.array[i]<<endl;
}
cout << "Press the enter key to continue ...";
cin.get();
return EXIT_SUCCESS;
}
编译运行结果如下:
为什么class可以用来定义模板参数呢?为什么还要引进typename呢?
1. 模板最初的目标只是为了对类类型进行泛型操作的定义,因此用class关键字声明泛型类型;
2. 在之后的进化过程中发现了模板相互调用时产生的 :: 操作符的二义性;
3. 因此引入typename关键字是用于告诉编译器将 :: 符号后的标识符看作类型;
写个函数判断一个变量是否为指针吗?
C++编译器匹配示例
#include <cstdlib>
#include <iostream>
using namespace std;
int test(int i, int j)
{
cout<<"int test(int i, int j)"<<endl;
}
template<typename T>
T test(T i, T j)
{
cout<<"T test(T i, T j)"<<endl;
}
int test(...)
{
cout<<"int test(...)"<<endl;
}
int main(int argc, char *argv[])
{
int i = 0;
int j = 0;
test(i, j);
cout << "Press the enter key to continue ...";
cin.get();
return EXIT_SUCCESS;
}
编译运行结果如下:
函数模板与可变参数函数的化学反应
#include <cstdlib>
#include <iostream>
using namespace std;
template<typename T>
void isPtr(T*)
{
cout<<"void isPtr(T*)"<<endl;
}
void isPtr(...)
{
cout<<"void isPtr(...)"<<endl;
}
int main(int argc, char *argv[])
{
int* pi = NULL;
float* pf = NULL;
int i = 0;
int j = 0;
isPtr(pi);
isPtr(pf);
isPtr(i);
isPtr(j);
cout << "Press the enter key to continue ...";
cin.get();
return EXIT_SUCCESS;
}
编译运行结果如下:
解决方案1
#include <cstdlib>
#include <iostream>
using namespace std;
template<typename T>
bool isPtr(T*)
{
return true;
}
bool isPtr(...)
{
return false;
}
int main(int argc, char *argv[])
{
int* pi = NULL;
float* pf = NULL;
int i = 0;
int j = 0;
cout<<isPtr(pi)<<endl;
cout<<isPtr(pf)<<endl;
cout<<isPtr(i)<<endl;
cout<<isPtr(j)<<endl;
cout << "Press the enter key to continue ...";
cin.get();
return EXIT_SUCCESS;
}
编译运行结果如下:
你的方法实现了指针的判断,但是我觉得不够高效,你有更好的办法吗?
解决方案1已经很好的解决问题,那么为什么还不够高效呢?哪里不够高效呢?
解决方案1中唯一耗时的地方在于函数调用的建栈与退栈过程,因此需要考虑如何避免这个过程以提高程序效率。
解决方案2
#include <cstdlib>
#include <iostream>
using namespace std;
template<typename T>
char isPtr(T*);
int isPtr(...);
#define ISPTR(v) (sizeof(isPtr(v)) == sizeof(char))
int main(int argc, char *argv[])
{
int* pi = NULL;
float* pf = NULL;
int i = 0;
int j = 0;
cout<<ISPTR(pi)<<endl;
cout<<ISPTR(pf)<<endl;
cout<<ISPTR(i)<<endl;
cout<<ISPTR(j)<<endl;
cout << "Press the enter key to continue ...";
cin.get();
return EXIT_SUCCESS;
}
编译运行结果如下: