From a43285d95ab0820ca590565aacc9f6cf2b25174c Mon Sep 17 00:00:00 2001 From: Werner Henze <34543625+beinhaerter@users.noreply.github.com> Date: Thu, 12 Oct 2023 21:23:59 +0200 Subject: [PATCH] Make example code adhere to C.65 (#2151) C.65 requires move assignment to be safe for self-assignment. The given example is not safe for self-assignment as given right now. This commit fixes this. Co-authored-by: Werner Henze --- CppCoreGuidelines.md | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/CppCoreGuidelines.md b/CppCoreGuidelines.md index f96aa63..1ab841f 100644 --- a/CppCoreGuidelines.md +++ b/CppCoreGuidelines.md @@ -6438,9 +6438,11 @@ A non-throwing move will be used more efficiently by standard-library and langua public: Vector(Vector&& a) noexcept :elem{a.elem}, sz{a.sz} { a.elem = nullptr; a.sz = 0; } Vector& operator=(Vector&& a) noexcept { - delete elem; - elem = a.elem; a.elem = nullptr; - sz = a.sz; a.sz = 0; + if (&a != this) { + delete elem; + elem = a.elem; a.elem = nullptr; + sz = a.sz; a.sz = 0; + } return *this; } // ...