在C ++ 11中'typedef'和'using'有什么区别? [英] What is the difference between 'typedef' and 'using' in C++11?

查看:133
本文介绍了在C ++ 11中'typedef'和'using'有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道在C ++ 11中,我们现在可以使用使用写入类型别名,例如 typedef

I know that in C++11 we can now use using to write type alias, like typedefs:

typedef int MyInt;

根据我的理解,等同于:

Is, from what I understand, equivalent to:

using MyInt = int;

新的语法出现在努力表达 template typedef

And that new syntax emerged from the effort to have a way to express "template typedef":

template< class T > using MyType = AnotherType< T, MyAllocatorType >;

但是,前两个非模板示例,标准中是否有其他细微差别?例如, typedef s以弱的方式进行别名操作。这是它不创建一个新的类型,但只有一个新的名称(转换是隐含在这些名称之间)。

But, with the first two non-template examples, are there any other subtle differences in the standard? For example, typedefs do aliasing in a "weak" way. That is it does not create a new type but only a new name (conversions are implicit between those names).

使用相同还是生成新类型?

推荐答案

它们是相同的,从标准(强调我的)(7.1.3.2):

They are equivalent, from the standard (emphasis mine) (7.1.3.2):


typedef名称也可以通过别名声明引入。 using关键字之后的
标识符成为typedef名称,并且标识符后面的
可选属性 - 说明符seq在该typedef名称之后为
它具有与由typedef说明符引入的
相同的语义
。特别地,它
不定义一个新类型,它不会出现在type-id 。

A typedef-name can also be introduced by an alias-declaration. The identifier following the using keyword becomes a typedef-name and the optional attribute-specifier-seq following the identifier appertains to that typedef-name. It has the same semantics as if it were introduced by the typedef specifier. In particular, it does not define a new type and it shall not appear in the type-id.

这篇关于在C ++ 11中'typedef'和'using'有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆