From 64ab13b3d0376f73905429d2aa6199d54f8aef0e Mon Sep 17 00:00:00 2001 From: Herb Sutter Date: Thu, 18 Feb 2021 11:20:09 -0800 Subject: [PATCH] Closes #1748 --- CppCoreGuidelines.md | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/CppCoreGuidelines.md b/CppCoreGuidelines.md index 2d5b6e2..d7c74bd 100644 --- a/CppCoreGuidelines.md +++ b/CppCoreGuidelines.md @@ -4669,30 +4669,26 @@ If you don't want a generated default function, suppress it with `=delete`. ##### Example, good When a destructor needs to be declared just to make it `virtual`, it can be -defined as defaulted. To avoid suppressing the implicit move operations -they must also be declared, and then to avoid the class becoming move-only -(and not copyable) the copy operations must be declared: +defined as defaulted. class AbstractBase { public: - virtual ~AbstractBase() = default; - AbstractBase(const AbstractBase&) = default; - AbstractBase& operator=(const AbstractBase&) = default; - AbstractBase(AbstractBase&&) = default; - AbstractBase& operator=(AbstractBase&&) = default; + virtual ~AbstractBase() = default; + // ... }; -Alternatively to prevent slicing as per [C.67](#Rc-copy-virtual), -the copy and move operations can all be deleted: +To prevent slicing as per [C.67](#Rc-copy-virtual), +`=delete` the copy and move operations and add a `clone`: class ClonableBase { public: - virtual unique_ptr clone() const; - virtual ~ClonableBase() = default; - ClonableBase(const ClonableBase&) = delete; - ClonableBase& operator=(const ClonableBase&) = delete; - ClonableBase(ClonableBase&&) = delete; - ClonableBase& operator=(ClonableBase&&) = delete; + virtual unique_ptr clone() const; + virtual ~ClonableBase() = default; + ClonableBase(const ClonableBase&) = delete; + ClonableBase& operator=(const ClonableBase&) = delete; + ClonableBase(ClonableBase&&) = delete; + ClonableBase& operator=(ClonableBase&&) = delete; + // ... other constructors and functions ... }; Defining only the move operations or only the copy operations would have the