C++11 使用using替换typedef来给模板取别名

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无法做到

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值