斗士狗 2012-05-25 02:39 采纳率: 0%
浏览 426
已采纳

在 c + + 11中'typedef'和'using'之间有什么区别?

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;

And that new syntax emerged from the effort to have a way to express "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, 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).

Is it the same with using or does it generate a new type? Are there any differences?

转载于:https://stackoverflow.com/questions/10747810/what-is-the-difference-between-typedef-and-using-in-c11

  • 写回答

4条回答 默认 最新

  • 叼花硬汉 2012-05-25 03:16
    关注

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

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!