C ++ core principles C.65: let mobile operating safety of the self-assignment

C.65: Safe for the Make the Move the Assignment the Assignment Self-
C.65: let mobile operating safety of the self-assignment

 

 

Reason (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.

If x = x changed the value of x, people will be surprised and potentially serious errors. Although people generally do not use direct assignment from the move operation, it will still happen. However, due to std :: swap is implemented as a mobile operation, if you accidentally call swap (a, b) and a and b refer to the same object, if there is no deal with self-assignment, it may be severe and difficult to find mistake.

 

 

 

Example (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.

The probability of violation of self-assignment checking one in a million (data as long as understand the spirit of good, Translator's Note); avoid discussion of the relationship between self-evaluation and self moving closer.

 

 

 

Note (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).

Not be avoided if (this == & a) return * this; universal approach operation. The method of checking mobile assignments will still arrive at a correct result (for example, the value of x will not change after x = x).

 

 

 

Note (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 standards only for the standard library is easy to ensure a "legal but undefined" state. It does not seem to be a problem in 10 years of experience and product applications. If you encounter a counterexample, please contact the editor. This rule is more cautious and insisted on complete safety.

 

 

 

Example (Example)

Here is a way to move a pointer without a test (imagine it as code in the implementation a move assignment):

There is not a method to check the movement of the pointer (think of it as part of a mobile implement assignment code):

// move from other.ptr to this->ptr
T* temp = other.ptr;
other.ptr = nullptr;
delete ptr;
ptr = temp;

 

Translator's Note

If this is the same and other objects, other.ptr = nullptr this.ptr will also be blank, resulting in the following delete ptr ineffective. This ensures the safety of self-moving.

This practice is too difficult to understand.

 

 

Enforcement & (Example)

  • (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.

  • (Medium) in the case of self-assignment, the operator should avoid moving operation object pointer points to an object is destroyed member or members of the pointer is set to null.

  • (Not enforceable) Look at the use of standard-library container types (incl. string) and consider them safe for ordinary (not life-critical) uses.

  • (Not implemented) to find using standard library container types (including string), think they (not declared sensitive period) when the general-purpose security.

 

 

Description link

https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#c65-make-move-assignment-safe-for-self-assignment

 


 

I think this article helpful? Welcome thumbs up and share it with more people.

Read more updated articles, please pay attention to micro-channel public number of object-oriented thinking []

发布了408 篇原创文章 · 获赞 653 · 访问量 29万+

Guess you like

Origin blog.csdn.net/craftsman1970/article/details/104737111