首页 文章详情

C++核心准则T.13:对于简单的,单类型参数概念,使用缩略记法更好

面向对象思考 | 267 2020-08-26 05:16 0 0 0
UniSMS (合一短信)

月季

T.13: Prefer the shorthand notation for simple, single-type argument concepts

T.13:对于简单的,单类型参数概念,使用缩略记法更好


Reason(原因)

Readability. Direct expression of an idea.

可读性。直接表现想法。


Example (using TS concepts)示例(使用TS概念)

To say "T is Sortable":

为了表达“T是可排序类型”:

template       // Correct but verbose: "The parameter is
// requires Sortable // of type T which is the name of a type
void sort(T&); // that is Sortable"

template // Better (assuming support for concepts): "The parameter is of type T
void sort(T&); // which is Sortable"

void sort(Sortable&); // Best (assuming support for concepts): "The parameter is Sortable"

The shorter versions better match the way we speak. Note that many templates don't need to use the template keyword.

较短的版本更符合我们想要表达的。注意很多模板不需要使用模板关键字。


Note(注意)

"Concepts" are defined in an ISO Technical Specification: concepts. A draft of a set of standard-library concepts can be found in another ISO TS: ranges Concepts are supported in GCC 6.1 and later. Consequently, we comment out uses of concepts in examples; that is, we use them as formalized comments only. If you use a compiler that supports concepts (e.g., GCC 6.1 or later), you can remove the //

“概念”ISO技术规格concepts中被定义。一套标准库concepts的初步版本可以在另一个ISO技术规格:ranges中找到。GCC6.1以后都支持concepts。因此我们在实例代码中注释掉使用concepts的部分;也就是说我们只是将它们用作标准的注释。如果你使用GCC6.1之后的版本,可以打开注释。


Enforcement(实施建议)

  • Not feasible in the short term when people convert from the  and  notation.

  • 如果人们从记法转过来,使用缩略记法是不合适的。

  • Later, flag declarations that first introduce a typename and then constrain it with a simple, single-type-argument concept.

  • 随后,标记第一次引入类型名并马上使用简单的,单类型概念对其进行约束的情况。


原文链接

https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#t13-prefer-the-shorthand-notation-for-simple-single-type-argument-concepts


新书介绍

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

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

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




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

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

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



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