T.21: Require a complete set of operations for a concept
T.21:为概念定义一套完整的操作
Reason(原因)
Ease of comprehension. Improved interoperability. Helps implementers and maintainers.
容易理解。提高相互操作性。帮助实现者和维护者。
Note(注意)
This is a specific variant of the general rule that a concept must make semantic sense.
这是通用规则:避免定义没有明确语义的“概念”的特化版本。
Example, bad (using TS concepts)(反面示例(使用TS概念))
This makes no semantic sense. You need at least + to make - meaningful and useful.
这段代码不具语义感觉。你至少需要一个+操作让-操作有意义和有用。
Examples of complete sets are
完整组合的例子有:
-
Arithmetic: +, -, *, /, +=, -=, *=, /= -
Comparable: <, >, <=, >=, ==, !=
Note(注意)
This rule applies whether we use direct language support for concepts or not. It is a general design rule that even applies to non-templates:
无论你是否使用的语言对概念直接支持,本规则都适用。这是一条通用的设计原则,即使对于非模板编程也是如此。
This is minimal, but surprising and constraining for users. It could even be less efficient.
这是最小化实现,但是会让使用者感到诧异和受限。它甚至可能是非效率的。
The rule supports the view that a concept should reflect a (mathematically) coherent set of operations.
本规则支持这样的视角:概念应该反映(算数)一套合理的操作组合。
Example(示例)
It can be a nuisance to define all operators, but not hard. Ideally, that rule should be language supported by giving you comparison operators by default.
定义所有的操作是一件麻烦的事,但并不困难。理想情况下,语言应该通过默认提供比较操作的方式支持本规则。
Enforcement(实施建议)
- Flag classes that support "odd" subsets of a set of operators, e.g., == but not != or + but not -. Yes, std::string is "odd", but it's too late to change that.
- 标记只支持一套运算符的怪异子集的情况,例如支持==但不支持!=,或者支持+但不支持-。没错,std::string就很怪异,但现在修改它已经太晚了。
原文链接
https:///isocpp/CppCoreGuidelines/blob/master/#t11-whenever-possible-use-standard-concepts
新书介绍
《实战Python设计模式》是作者最近出版的新书,拜托多多关注!

本书利用Python 的标准GUI 工具包tkinter,通过可执行的示例对23 个设计模式逐个进行说明。这样一方面可以使读者了解真实的软件开发工作中每个设计模式的运用场景和想要解决的问题;另一方面通过对这些问题的解决过程进行说明,让读者明白在编写代码时如何判断使用设计模式的利弊,并合理运用设计模式。
对设计模式感兴趣而且希望随学随用的读者通过本书可以快速跨越从理解到运用的门槛;希望学习Python GUI 编程的读者可以将本书中的示例作为设计和开发的参考;使用Python 语言进行图像分析、数据处理工作的读者可以直接以本书中的示例为基础,迅速构建自己的系统架构。
觉得本文有帮助?请分享给更多人。
关注微信公众号【面向对象思考】轻松学习每一天!
面向对象开发,面向对象思考!

















