C++11中引入了新的类型别名定义方式
using identifier = type-id
曾经在C++中定义类型的别名是采用
typedef identifier type-id;
如果对于通常状况下的类型进行别名声明,两种方式没有区别,可能C++11新引入的方式比较容易理解 。传统的typedef可能会将原类型和新类型的位置弄混,采用=类似于赋值操作更好理解。
别名模板 类型别名可以用于隐藏模板参数
// 这里PRT为const char* template using PTR = T*; PTR str = "Hello World!"; // 少写几个int参数 template struct Alloc { }; template using Vec = vector >; // type-id is vector > Vec v; // Vec is the same as vector >
别名模板不能用自身的类型
templatestruct A; template using B = typename A ::U; // type-id is A ::U template struct A { typedef B U; }; B b; // error: B uses its own type via A ::U



