首页 文章详情

C++核心准则T.26:相比简单的语法,根据使用模式术语定义概念更好

面向对象思考 | 362 2020-09-01 21:27 0 0 0
UniSMS (合一短信)

大连 书香园

T.26: Prefer to define concepts in terms of use-patterns rather than simple syntax

T.26:相比简单的语法,根据使用模式术语定义概念更好


Reason(原因)

The definition is more readable and corresponds directly to what a user has to write. Conversions are taken into account. You don't have to remember the names of all the type traits.

这种定义方式更具可读性,和用户必须写的内容直接对应。转换也考虑在内。你不必记住所有类型的特征。


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

You might be tempted to define a concept Equality like this:

你可能试图像这样定义Equality概念:

template concept Equality = has_equal && has_not_equal;

Obviously, it would be better and easier just to use the standard EqualityComparable, but - just as an example - if you had to define such a concept, prefer:

显然,只是使用标准的EqualityComparable会更好,也更容易,但是只是一个例子,如果你必须定义这样的概念,这样更好:

template concept Equality = requires(T a, T b) {
bool == { a == b }
bool == { a != b }
// axiom { !(a == b) == (a != b) }
// axiom { a = b; => a == b } // => means "implies"
}

as opposed to defining two meaningless concepts has_equal and has_not_equal just as helpers in the definition of Equality. By "meaningless" we mean that we cannot specify the semantics of has_equal in isolation.

而不是只为了辅助定义Equality而定义无意义的has_equal和has_not_equal概念。通过“无意义”这个词,我们想表达的是我们无法独立地定义has_equal的语义。


Enforcement(实施建议)

???

原文链接

https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#t26-prefer-to-define-concepts-in-terms-of-use-patterns-rather-than-simple-syntax


新书介绍

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

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

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




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

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

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



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