C++中为什么定义别名时using比typedef更好
这篇文章主要介绍"C++中为什么定义别名时using比typedef更好",在日常操作中,相信很多人在C++中为什么定义别名时using比typedef更好问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答"C++中为什么定义别名时using比typedef更好"的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
T.43: 定义别名时,using比typedef更好
Reason(原因)
Improved readability: With using, the new name comes first rather than being embedded somewhere in a declaration. Generality: using can be used for template aliases, whereas typedefs can't easily be templates. Uniformity: using is syntactically similar to auto.
提高可读性:使用using,新名称最先出现,而不是嵌入在声明的某个地方。通用性:using可以用于模板别名,然而typedef无法简单地用于模板。统一性:using在句法上和auto相似。
Example(示例)
typedef int (*PFI)(int); // OK, but convoluted
using PFI2 = int (*)(int); // OK, preferred
template
typedef int (*PFT)(T); // error
template
using PFT2 = int (*)(T); // OK
Enforcement(实施建议)
Flag uses of typedef. This will give a lot of "hits" :-(
标记使用typedef的地方。会有发现大量使用typedef的代码:-(
到此,关于"C++中为什么定义别名时using比typedef更好"的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注网站,小编会继续努力为大家带来更多实用的文章!