Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
252 views
in Technique[技术] by (71.8m points)

c++ - C ++ 11中的'typedef'和'using'有什么区别?(What is the difference between 'typedef' and 'using' in C++11?)

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

(我知道在C ++ 11中,我们现在可以使用using来编写类型别名,例如typedef :)

typedef int MyInt;

Is, from what I understand, equivalent to:

(据我了解,相当于:)

using MyInt = int;

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

(并且,这种新语法是通过努力表达“ template typedef ”的方式而出现的:)

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

But, with the first two non-template examples, are there any other subtle differences in the standard?

(但是,对于前两个非模板示例,标准中是否还有其他细微差别?)

For example, typedef s do aliasing in a "weak" way.

(例如, typedef会以“弱”方式进行别名。)

That is it does not create a new type but only a new name (conversions are implicit between those names).

(也就是说,它不会创建新的类型,而只会创建一个新名称(这些名称之间的转换是隐式的)。)

Is it the same with using or does it generate a new type?

(是否与using相同或会生成新类型?)

Are there any differences?

(有什么区别吗?)

  ask by Klaim translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

(它们与标准(强调我的)(7.1.3.2)是等效的:)

A typedef-name can also be introduced by an alias-declaration.

(typedef名称也可以通过别名声明来引入。)

The identifier following the using keyword becomes a typedef-name and the optional attribute-specifier-seq following the identifier appertains to that typedef-name.

(using关键字后面的标识符变为typedef名称,而标识符后面的可选attribute-specifier-seq属于该typedef名称。)

It has the same semantics as if it were introduced by the typedef specifier.

(它具有与typedef说明符引入的语义相同的语义。)

In particular, it does not define a new type and it shall not appear in the type-id.

(特别是,它没有定义新的类型,也不应出现在type-id中。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...