C++核心準(zhǔn)則T.21:為概念定義一套完整的操作

大連騰飛軟件園2期
T.21: Require a complete set of operations for a concept
T.21:為概念定義一套完整的操作
Reason(原因)
Ease of comprehension. Improved interoperability. Helps implementers and maintainers.
容易理解。提高相互操作性。幫助實(shí)現(xiàn)者和維護(hù)者。
Note(注意)
This is a specific variant of the general rule that?a concept must make semantic sense.
這是通用規(guī)則:避免定義沒(méi)有明確語(yǔ)義的“概念”的特化版本。
Example, bad (using TS concepts)(反面示例(使用TS概念))
template concept Subtractable = requires(T a, T, b) { a-b; }; This makes no semantic sense. You need at least?+?to make?-?meaningful and useful.
這段代碼不具語(yǔ)義感覺(jué)。你至少需要一個(gè)+操作讓-操作有意義和有用。
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:
無(wú)論你是否使用的語(yǔ)言對(duì)概念直接支持,本規(guī)則都適用。這是一條通用的設(shè)計(jì)原則,即使對(duì)于非模板編程也是如此。
class Minimal {
// ...
};
bool operator==(const Minimal&, const Minimal&);
bool operator<(const Minimal&, const Minimal&);
Minimal operator+(const Minimal&, const Minimal&);
// no other operators
void f(const Minimal& x, const Minimal& y)
{
if (!(x == y)) { /* ... */ } // OK
if (x != y) { /* ... */ } // surprise! error
while (!(x < y)) { /* ... */ } // OK
while (x >= y) { /* ... */ } // surprise! error
x = x + y; // OK
x += y; // surprise! error
}
This is minimal, but surprising and constraining for users. It could even be less efficient.
這是最小化實(shí)現(xiàn),但是會(huì)讓使用者感到詫異和受限。它甚至可能是非效率的。
The rule supports the view that a concept should reflect a (mathematically) coherent set of operations.
本規(guī)則支持這樣的視角:概念應(yīng)該反映(算數(shù))一套合理的操作組合。
Example(示例)
class Convenient {
// ...
};
bool operator==(const Convenient&, const Convenient&);
bool operator<(const Convenient&, const Convenient&);
// ... and the other comparison operators ...
Minimal operator+(const Convenient&, const Convenient&);
// .. and the other arithmetic operators ...
void f(const Convenient& x, const Convenient& y)
{
if (!(x == y)) { /* ... */ } // OK
if (x != y) { /* ... */ } // OK
while (!(x < y)) { /* ... */ } // OK
while (x >= y) { /* ... */ } // OK
x = x + y; // OK
x += y; // OK
}
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.
定義所有的操作是一件麻煩的事,但并不困難。理想情況下,語(yǔ)言應(yīng)該通過(guò)默認(rèn)提供比較操作的方式支持本規(guī)則。
Enforcement(實(shí)施建議)
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.
標(biāo)記只支持一套運(yùn)算符的怪異子集的情況,例如支持==但不支持!=,或者支持+但不支持-。沒(méi)錯(cuò),std::string就很怪異,但現(xiàn)在修改它已經(jīng)太晚了。
原文鏈接
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#t21-require-a-complete-set-of-operations-for-a-concept
新書介紹
《實(shí)戰(zhàn)Python設(shè)計(jì)模式》是作者最近出版的新書,拜托多多關(guān)注!

本書利用Python 的標(biāo)準(zhǔn)GUI 工具包tkinter,通過(guò)可執(zhí)行的示例對(duì)23 個(gè)設(shè)計(jì)模式逐個(gè)進(jìn)行說(shuō)明。這樣一方面可以使讀者了解真實(shí)的軟件開發(fā)工作中每個(gè)設(shè)計(jì)模式的運(yùn)用場(chǎng)景和想要解決的問(wèn)題;另一方面通過(guò)對(duì)這些問(wèn)題的解決過(guò)程進(jìn)行說(shuō)明,讓讀者明白在編寫代碼時(shí)如何判斷使用設(shè)計(jì)模式的利弊,并合理運(yùn)用設(shè)計(jì)模式。
對(duì)設(shè)計(jì)模式感興趣而且希望隨學(xué)隨用的讀者通過(guò)本書可以快速跨越從理解到運(yùn)用的門檻;希望學(xué)習(xí)Python GUI 編程的讀者可以將本書中的示例作為設(shè)計(jì)和開發(fā)的參考;使用Python 語(yǔ)言進(jìn)行圖像分析、數(shù)據(jù)處理工作的讀者可以直接以本書中的示例為基礎(chǔ),迅速構(gòu)建自己的系統(tǒng)架構(gòu)。
覺(jué)得本文有幫助?請(qǐng)分享給更多人。
關(guān)注微信公眾號(hào)【面向?qū)ο笏伎肌枯p松學(xué)習(xí)每一天!
面向?qū)ο箝_發(fā),面向?qū)ο笏伎迹?/span>
