本小节回顾的知识点是using定义模板别名。
using(来)定义模板的别名:
在介绍今天要总结的using关键字的“新”功能前,先来介绍一下typedef关键字的用法:
typedef : 一般是用来定义类型别名(也即给类型起一个别名)
格式:
typedef Type TypeName;//给类型Type起一个别名TypeName
typedef retName (*pointerName)(FuncParams);//定义一种指向retName(*)(FuncParams)类型函数的函数指针
比如:
typedef unsigned int uint_t;// ==> 给unsigned int类型起了一个别名uint;
typedef std::unordered_map<std::string, int> unordered_map_str_int;
// ==> 给unordered_map<std::string, int>类型起了一个别名unordered_map_str_int;
typedef int(*FuncType)(int,int);//定义了一个指向int(*)(int,int)类型函数的函数指针
int main(void) {
uint_t abc = 0;// ==> unsigned int abc = 0;
unordered_map_str_int ummp;// ==> unordered_map<std::string, int> ummp;
ummp.insert({ "lzf",23 });
ummp["lyf"] = 18;
return 0;
}
有时候,我们想让这种类型中的参数不是固定,就比如我们想这么干:
std::unordered_map<std::string, T> unordered_map_str_int;
让哈希表的key值保持不变,但我想让是value值是一个模板值(适合于各种类型的值)。显然,用typedef就是不行的!因为typedef只能给固定的类型起别名!起了<string,int>在类型的别名后后序使用时就不能变了,你要想使用<string,string>类型的哈希表则需要这么干:
std::unordered_map<std::string, std::string> unmmp;
//or
typedef std::unordered_map<std::string, std::string> unordered_map_str_str;
unordered_map_str_str unmmp;
在C++98标准的年代,为了deal这种问题,就使用了一种比较“猥琐”的方式来达到该目的。
请看以下代码:
template<typename st>
struct map_s{
typedef std::unordered_map<std::string, st> type;
//定义了一个std::unordered_map<std::string, st>类型的别名type
//key: std::string value: st(模板参数)
};
//or
template<typename st>
class map_s2 {
public:
typedef std::unordered_map<std::string, st> type;
//定义了一个std::unordered_map<std::string, st>类型的别名type
//key: std::string value: st(模板参数)
};
int main(void) {
map_s<int>::type unmap1;// ==> std::unordered_map<std::string, int> unmap1;
unmap1.insert({ "lzf",23 });
unmap1["lyf"] = 18;
map_s2<int>::type unmap2;// ==> std::unordered_map<std::string, int> unmap2;
unmap2.insert({ "lzf",23 });
unmap2["lyf"] = 18;
return 0;
}
那有没有更加简便一些的写法呢?答案毋庸置疑,是有的!C++11又引入了using关键字的另外一种用法:给一个模板类型起别名。怎么用呢?
格式:
template <typename T1,...>
using templateTypeName = templateType;//templateTypeName是templateType的别名!
请看以下代码:
template<typename T>
using map_ss = unordered_map<std::string, T>;
int main(void) {
map_ss<int> unmap;// ==> unordered_map<std::string, int> unmap;
unmap.insert({ "lzf",23 });
unmap["lyf"] = 18;
return 0;
}
注意:当using关键字用于定义类型别名时,其实是包含了typedef的所有功能的!也即,using关键字不仅能用于给模板类型定义别名,还能用于给普通的类型定义别名(typedef的功能)。
请看以下代码:
typedef unsigned int uint_t;
// ==>
using uint_t = unsigned int;
----------------------------
typedef std::unordered_map<std::string, int> unordered_map_str_int;
// ==>
using unordered_map_str_int = std::unordered_map<std::string, int>;
template<typename T>
using tfunc = int(*)(T, T);//用using定义一个函数指针模板类型tfunc!
//或者说给指向int(*)(T, T)这种函数模板的指针变量取个别名为tfunc!
int fadd(int a, int b) {
auto ret = a + b;
cout << a << " + " << b << " is " << a + b << endl;
return ret;
}
int main(void) {
tfunc<int> tfpoint = fadd;
//tfunc<int>是一个函数指针类型
//tfpoints是一个函数指针(变量)
//创建该函数指针变量时直接赋初值为fadd(这是一个函数首地址)
tfpoint(2, 8);
tfpoint(12, 18);
return 0;
}
运行结果:
小结:用using关键字来定义类型模板的别名(C++11引入的),我们可认为这就是一种新的模板类型:类型模板。(既不是类模板,也不是函数模板)。
以上就是我总结的关于using定义模板别名的笔记。希望你能读懂并且消化完,也希望自己能牢记这些小小的细节知识点,加油吧,我们都在coding的路上~