C++核心準(zhǔn)則C.65:讓移動(dòng)操作對(duì)自賦值安全

白鎢礦
C.65:讓移動(dòng)操作對(duì)自賦值安全

Reason(原因)
If x = x changes the value of x, people will be surprised and bad errors may occur. However, people don't usually directly write a self-assignment that turn into a move, but it can occur. However, std::swap is implemented using move operations so if you accidentally do swap(a, b) where a and b refer to the same object, failing to handle self-move could be a serious and subtle error.
如果x=x改變了x的值,人們會(huì)感到詫異而且有可能發(fā)生嚴(yán)重的錯(cuò)誤。雖然人們一般不會(huì)直接在移動(dòng)操作中使用自賦值,它還是會(huì)發(fā)生。但是由于std::swap被實(shí)現(xiàn)為使用移動(dòng)操作,如果你意外地調(diào)用了swap(a,b)而a和b參照了同一個(gè)對(duì)象,如果沒有處理好自賦值的話,可能會(huì)發(fā)生嚴(yán)重且不易發(fā)現(xiàn)的錯(cuò)誤。

Example(示例)
class Foo {
? ?string s;
? ?int i;
public:
? ?Foo& operator=(Foo&& a);
? ?// ...
};
Foo& Foo::operator=(Foo&& a) noexcept ?// OK, but there is a cost
{
if (this == &a) return *this; ?// this line is redundant
? ?s = std::move(a.s);
? ?i = a.i;
? ?return *this;
}The ?one-in-a-million argument againstif (this == &a) return *this; tests from the discussion of self-assignment is even more relevant for self-move.
違反自我賦值檢查的概率只有百萬分之一(數(shù)據(jù)只要領(lǐng)會(huì)精神就好,譯者注);避免自我賦值的討論和自我移動(dòng)的關(guān)系更加密切。

Note(注意)
There is no known general way of avoiding an if (this == &a) return *this; test for a move assignment and still get a correct answer (i.e., after x = x the value of x is unchanged).
沒有避免使用if(this==&a)return *this;操作的普遍辦法。檢查移動(dòng)賦值的方法仍然會(huì)得出正確的結(jié)果(例如在x=x之后x的值不會(huì)改變)。

Note(注意)
The ISO standard guarantees ?only a "valid but unspecified" state for the standard-library containers. Apparently this has not been a problem in about 10 years of experimental and production use. Please contact the editors if you find a counter example. The rule here is more caution and insists on complete safety.
ISO標(biāo)準(zhǔn)只為標(biāo)準(zhǔn)庫(kù)容易保證了一個(gè)“合法但未定義”的狀態(tài)??雌饋碓?0多年的經(jīng)驗(yàn)和產(chǎn)品應(yīng)用中沒有成為問題。如果你遇到了反例請(qǐng)聯(lián)系編輯。本規(guī)則更為謹(jǐn)慎并堅(jiān)持做到完全安全。

Example(示例)
Here is a way to move a pointer without a test (imagine it as code in the implementation a move assignment):
這里有一個(gè)不檢查就移動(dòng)指針的方法(將其想象為實(shí)現(xiàn)移動(dòng)賦值代碼中的一部分):
// move from other.ptr to this->ptr
T* temp = other.ptr;
other.ptr = nullptr;
delete ptr;
ptr = temp;譯者注
如果other和this是同一個(gè)對(duì)象,other.ptr=nullptr也會(huì)同時(shí)將this.ptr置空,導(dǎo)致下面的delete ptr不起作用。這樣就保證了自我移動(dòng)時(shí)的安全。
這種做法太難理解了。

Enforcement(示例)
(Moderate) In the case of self-assignment, a move assignment operator should not leave the object holding pointer members that have been deleted or set to nullptr.
(中等)在自賦值的情況下,移動(dòng)操作運(yùn)算符應(yīng)該避免對(duì)象的指針成員指向的對(duì)象被銷毀或者該指針成員被置空。
(Not enforceable) Look at the use of standard-library container types (incl. string) and consider them safe for ordinary (not life-critical) uses.
(無法實(shí)施)找到使用標(biāo)準(zhǔn)庫(kù)容器類型(包括string),認(rèn)為它們?cè)谕ǔS猛荆ú皇锹暶髦芷诿舾校r(shí)安全的。

原文鏈接
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#c65-make-move-assignment-safe-for-self-assignment
覺得本文有幫助?請(qǐng)分享給更多人。
關(guān)注【面向?qū)ο笏伎肌枯p松學(xué)習(xí)每一天!
面向?qū)ο箝_發(fā),面向?qū)ο笏伎迹?/span>
