C++11 模板的别名

【1】using关键字

C++11标准规定了一种新的方法,使用关键字using作为别名声明来定义类型的别名,其后紧跟别名和等号。

作用是把等号左侧的名字规定成等号右侧类型的别名。

(1)using用法与typedef相同点

代码示例如下:

 1 #include <iostream>
 2 #include <type_traits>
 3 using namespace std;
 4 
 5 using uint = unsigned int;
 6 typedef unsigned int UINT;
 7 
 8 int main()
 9 {
10     cout << is_same<uint, UINT>::value << endl; // 1
11 }

(2)using与typedef不同点(优势)

由于模板不是一个类型,所以不能定义一个typedef引用一个模板,但是新标准允许使用using为类模板定义一个别名:

1 template <typename T> using twin = pair<T, T>;
2 twin<string> authors;           // authors是一个pair<string, string>

good good study, day day up.

顺序 选择 循环 总结

猜你喜欢

转载自www.cnblogs.com/Braveliu/p/12241975.html