用法
有时候模板名称很长,我们想用一个别名替代这串模板声明:
看似好像跟#define一样,就像下面这样:
| 1
 | #define Vec<T> template<typename T> std::vector<T,MyAlloc<T>>
 | 
于是上面这个#define会被替换成
| 12
 3
 4
 
 | Vec<T> cpll;
 
 template<typaname int> std::vector<int, MyAlloc<int>> coll;
 
 | 
这是无法通过编译的,因为typedef是不接受参数的。
这时候我们就可以使用模板别名。如下面的形式:
| 12
 3
 4
 
 | template<typename T>using Vec = std::vector<T, MyAlloc<T>>
 
 Vec<int> coll;
 
 | 
我们还可以使用模板别名定义函数指针模板:
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 
 | template<typename T>using myfunc = int(*)(T, T);
 
 int Add(int a, int b){
 return a + b;
 }
 
 int main()
 {
 
 
 myfunc<int> pFunc;
 pFunc = Add;
 cout << pFunc(1, 2) << endl;
 return 0;
 }
 
 |