C++中,我们原本使用typedef为类型定义别名。
C++11中,我们倡导使用using来替代typedef为类型定义别名
typedef与using相同之处:都可以取别名
#include <iostream>
#include <type_traits>
using namespace std;
using uint = unsigned int;
typedef unsigned int UINT;
using sint = int;
int main()
{
cout << is_same<uint, UINT>::value << endl; // 1
}
// 编译选项:g++ -std=c++11 3-10-1.cpp
is_same是std中用于判断类型是否相同的模板类,模板类接收两个类型参数来实例化模板,其成员类型value输出结果,1则为相同。
不同之处(using 优势):using可以使用模板来取别名
template<typename T> using MapString = std::map<T, char*>;
MapString<int> numberedString; //numberdString<int, char*>
模板编程时,可使用using定义模板,而typedef无法做到