首页 文章详情

C++核心准则T.43: 定义别名时,using比typedef更好

面向对象思考 | 369 2020-09-07 02:14 0 0 0
UniSMS (合一短信)

T.43: Prefer using over typedef for defining aliases

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的代码:-(


原文链接

https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#t42-use-template-aliases-to-simplify-notation-and-hide-implementation-details


新书介绍

《实战Python设计模式》是作者最近出版的新书,拜托多多关注!

本书利用Python 的标准GUI 工具包tkinter,通过可执行的示例对23 个设计模式逐个进行说明。这样一方面可以使读者了解真实的软件开发工作中每个设计模式的运用场景和想要解决的问题;另一方面通过对这些问题的解决过程进行说明,让读者明白在编写代码时如何判断使用设计模式的利弊,并合理运用设计模式。

对设计模式感兴趣而且希望随学随用的读者通过本书可以快速跨越从理解到运用的门槛;希望学习Python GUI 编程的读者可以将本书中的示例作为设计和开发的参考;使用Python 语言进行图像分析、数据处理工作的读者可以直接以本书中的示例为基础,迅速构建自己的系统架构。




觉得本文有帮助?请分享给更多人。

关注微信公众号【面向对象思考】轻松学习每一天!

面向对象开发,面向对象思考!



good-icon 0
favorite-icon 0
收藏
回复数量: 0
    暂无评论~~
    Ctrl+Enter