C++核心準(zhǔn)則C.11:讓具體類型符合常規(guī)

C.11:?Make?concrete?types?regular
C.11:讓具體類型符合常規(guī)
Reason(原因)
Regular?types?are?easier?to?understand?and?reason?about?than?types?that?are?not?regular?(irregularities?requires?extra?effort?to?understand?and?use).
常規(guī)類型和非常規(guī)類型相比更容易理解和推測(cè)。理解和使用不符合常規(guī)的類型需要額外努力才行。
Example(示例)
struct Bundle {
string name;
vector vr;
};
bool operator==(const Bundle& a, const Bundle& b)
{
return a.name == b.name && a.vr == b.vr;
}
Bundle b1 { "my bundle", {r1, r2, r3}};
Bundle b2 = b1;
if (!(b1 == b2)) error("impossible!");
b2.name = "the other bundle";
if (b1 == b2) error("No!");
In?particular,?if?a?concrete?type?has?an?assignment also give it an equals operator so?that a?=?b?implies?a?==?b.
通常情況下,如果具體類型包含賦值操作,同時(shí)應(yīng)提供判斷相等的操作。也就是說(shuō)有a=b意味著也有a==b。
Note(注意)
Handles?for?resources?that?cannot?be?cloned,?e.g.,?a?scoped_lock?for?a?mutex,?resemble?concrete?types?in?that?they?most?often?are?stack-allocated.
However, objects of such types typically cannot be copied (instead, they can usually be moved),so they can't be regular; instead, they tend to be semiregular.Often,?such types are referred to as?"move-only?types".
管理不允許克隆的資源,例如用于管理mutex的scoped_lock,由于經(jīng)常在堆棧上分配,因此看起來(lái)像具體類型。但是這類類型的對(duì)象通常不能被拷貝(它們通常被移動(dòng)),因此它們不屬于常規(guī)的范疇,而是趨向于半常規(guī)。這樣的類型經(jīng)常被成為“只移動(dòng)類型”。
Enforcement(實(shí)施建議)
???
原文鏈接:
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#c11-make-concrete-types-regular
覺(jué)得本文有幫助?請(qǐng)分享給更多人。
關(guān)注【面向?qū)ο笏伎肌枯p松學(xué)習(xí)每一天!
面向?qū)ο箝_(kāi)發(fā),面向?qū)ο笏伎迹?/span>
